简体   繁体   中英

NoClassDefFoundError while running from jar

I am getting a "no class definition found" exception while trying to run my application on Windows (it runs fine on OS X). The classes the JVM is complaining about are my classes (no third party jars required). When I unzip the files inside the jar, all files are present, including the ones the JVMm is complaining about.

The jar is created using the following task:

<target name="jar" depends="">
<jar destfile="build/app.jar" > 
  <manifest>
    <attribute name="Built-By" value="hamza"/>
    <attribute name="Main-Class" value="com.hamza.driver.ui"/>
<attribute name="Class-Path" value="./"/>
  </manifest>
  <fileset dir="build">
    <include name="**/*.class"/>
<include name="**/*.png"/>
<include name="**/*.xpi"/>
<include name="**/*.html"/>
<exclude name="**/*.jar"/>
  </fileset>
</jar>

I cannot figure out what is causing the problem. If I unzip the jar and run the jar from the directory I unzipped the class to, everything works fine. So, I am assuming all the required files are inside the jar.

EDIT: com.hamza.driver.ui is a class in a package called com.hamza.driver which has main .

After the build, I get one jar "app.jar", and I run it using "java -jar app.jar", which executes fine on OS X, but not on Windows.

If I unzip app.jar in a seperate directory and run "java -jar app.jar", it excutes fine.

EDIT 2: exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/hamza/gui/tr
ansfer/ClipboardTransferHandle
        at com.hamza.driver.ui.main(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.hamza.gui.transfer.Clipboa
rdTransferHandle
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
        ... 1 more

ClipboardTransferHandle .class files are present in the jar.

EDIT 3: imports for the clip board class:

import java.util.logging.Logger;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.IOException;

While playing with it, I found that if I try to declare ClipboardTransferHandle as a static variable in the driver, it works, but every object that is not static is not found. All the main GUI elements are static variables, so the GUI is constructed, but other elements are not; everything that is created not static causes NoClassDefFound , but if I declare them static for testing, they work.

This is the problem that is occurring,

if the JAR file was loaded from "C:\\java\\apps\\appli.jar", and your manifest file has the Class-Path: reference "lib/other.jar", the class loader will look in "C:\\java\\apps\\lib\\" for "other.jar". It won't look at the JAR file entry "lib/other.jar".

Solution:-

  1. Right click on project, Select Export.
  2. Select Java Folder and in it select Runnable JAR File instead of JAR file.
  3. Select the proper options and in the Library Handling section select the 3rd option ie (Copy required libraries into a sub-folder next to the generated JAR).
  4. Click finish and your JAR is created at the specified position along with a folder that contains the JARS mentioned in the manifest file.
  5. open the terminal,give the proper path to your jar and run it using this command java -jar abc.jar

    Now what will happen is the class loader will look in the correct folder for the referenced JARS since now they are present in the same folder that contains your app JAR..There is no "java.lang.NoClassDefFoundError" exception thrown now.

    This worked for me... Hope it works you too!!!

Which class is missing? Your Main-Class attribute looks a little suspect--is com.hamza.driver.ui a class or a package?

There is a chance, that the NoClassDefFoundError (I really hate this error - always drives me crazy...) isn't thrown because it doesn't find the class it tells you (-> your class) but because java can't find one of the classes that are used to instantiate that class.

I had this problem once, when a class imported another class from a different jar (in my case: an OSGi bundle) which hadn't been properly exported. Although this was a OSGi specific problem - You may have the same problems in your environment. Maybe your application depends on some classes that are present in your actual OS-X environment but not in the actual Window environment. I'm not looking at third-party libraries but at the Java implementations itself.

Good luck!

Edit

There are two more quite similar question on SO, unfortunately with no accepted solution, but maybe one of the hints over there can help in your case:

NoClassDefFound when running a jar

NoClassDefFoundError inside jar

Edit 2

Here's a similiar problem that has an accepted answer. Hope this one helps:

NoClassDefFoundError while trying to run my jar with java.exe -jar...what's wrong?

您是否在类路径中指定了新jar(java -cp .; new.jar MainClass.class)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM