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

35,857

Solution 1

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.

Solution 2

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.

Share:
35,857
Roman
Author by

Roman

Updated on July 09, 2022

Comments

  • Roman
    Roman almost 2 years

    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?

  • V G
    V G over 10 years
    Alternatively refresh all the files in eclipse (Shortcut F5 on the root folder in your project).
  • Rudolf Real
    Rudolf Real over 10 years
    I tried this and many other alternatives and didn't work. Deleting the workspace only worked for me.
  • Rudolf Real
    Rudolf Real over 10 years
    I had the same issue with Mavericks. I only could solve it by deleting the workspace.
  • IndrekV
    IndrekV almost 10 years
    Also tried to re-add the src folder, but didn't work. Creating a new workspace did the trick.