Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Exception in thread "main" java.lang.NoClassDefFoundError:?

I run my software through Eclipse. Yesterday everything was fine. I made not changes to the code but today, when I am trying to run it again I get the following error messages:

Exception in thread "main" java.lang.NoClassDefFoundError: coloredtrails/CTListener
    at test.DemoPlayer1.createAndShowGUI(DemoPlayer1.java:23)
    at test.DemoPlayer1.main(DemoPlayer1.java:39)
Caused by: java.lang.ClassNotFoundException: coloredtrails.CTListener
    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)
    ... 2 more

Why it does not see the class? What could be the reason of that? How can I resolve the problem?

like image 584
Roman Avatar asked May 25 '11 12:05

Roman


People also ask

Why do I get NoClassDefFoundError?

The NoClassDefFoundError is a runtime error in Java that occurs if the Java Virtual Machine (JVM) or a ClassLoader instance attempts to load the definition of a class that could not be found. The class definition exists at compile-time but is not available at runtime.


2 Answers

A NoClassDefFoundError (almost) always means that your classpath is wrong. Make sure that your classpath includes the base directory of the coloredtrails package. (Ofcourse, also make sure that the file coloredtrails\CTListener.class actually exists).

When running from the command line:

You can set the classpath by setting the CLASSPATH environment variable, or by specifying it with the -cp or -classpath option on the command line when you run your program. For example:

java -cp C:\MyProject\classes coloredtrails.CTListener

edit - Looking at the stack trace and seeing URLClassLoader in there makes me think that you are trying to run a Java applet. To learn how to correctly deploy applets, so that all classes the applet needs can be found, see this tutorial: Deploying an Applet.

like image 112
Jesper Avatar answered Sep 22 '22 06:09

Jesper


Sometimes, my Eclipse (Indigo on MacOSX) does that, expecially if I do changes (removing files, moving them around) to the project structure on the filsystem directly.

Basically, eclipse cannot find the source folder anymore, so he doesn't compile the source but tries to run it anyway (all this without a warning or a reference to the problem).

To fix it, remove the source folder from the build path (=right click on the src folder under the project in the package explorer, then choose "Build-path->Remove from Build-path". Then, add it again (=right click on the folder under the project in the package explorer and choose "Add to build-path"). This makes the src folder "visible" to the compiler again and fixes the problem.

like image 30
Pezza Avatar answered Sep 21 '22 06:09

Pezza