Search code examples
javaeclipseclassnotfoundexception

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?


Solution

  • 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.