views:

705

answers:

3

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.

A: 

Have you specified the new jar in your classpath (java -cp .;new.jar MainClass.class)?

bwobbones
+1  A: 

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

Dominic Cooney
+1 believe this is the error, too
Daniel
A: 

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:

http://stackoverflow.com/questions/1074955/noclassdeffound-when-running-a-jar

http://stackoverflow.com/questions/959980/noclassdeffounderror-inside-jar

Edit 2

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

http://stackoverflow.com/questions/250166/noclassdeffounderror-while-trying-to-run-my-jar-with-java-exe-jar-whats-wrong

Andreas_D
but if that's the case, it shouldn't work when i unzip it. but it works.
Hamza Yerlikaya
Ah - OK, didn't read carefully enough.
Andreas_D