How can I solve "java.lang.NoClassDefFoundError"?

Solution 1:

After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the classloader (in this case java.net.URLClassLoader), which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use.

Your code wouldn't compile if the required classes weren't present (unless classes are loaded with reflection), so usually this exception means that your classpath doesn't include the required classes. Remember that the classloader (specifically java.net.URLClassLoader) will look for classes in package a.b.c in folder a/b/c/ in each entry in your classpath. NoClassDefFoundError can also indicate that you're missing a transitive dependency of a .jar file that you've compiled against and you're trying to use.

For example, if you had a class com.example.Foo, after compiling you would have a class file Foo.class. Say for example your working directory is .../project/. That class file must be placed in .../project/com/example, and you would set your classpath to .../project/.

Side note: I would recommend taking advantage of the amazing tooling that exists for Java and JVM languages. Modern IDEs like Eclipse and IntelliJ IDEA and build management tools like Maven or Gradle will help you not have to worry about classpaths (as much) and focus on the code! That said, this link explains how to set the classpath when you execute on the command line.

Solution 2:

I'd like to correct the perspective of others on NoClassDefFoundError.

NoClassDefFoundError can occur for multiple reasons like:

  1. ClassNotFoundException -- .class not found for that referenced class irrespective of whether it is available at compile time or not(i.e base/child class).
  2. Class file located, but Exception raised while initializing static variables
  3. Class file located, Exception raised while initializing static blocks

In the original question, it was the first case which can be corrected by setting CLASSPATH to the referenced classes JAR file or to its package folder.

What does it mean by saying "available in compile time"?

  • The referenced class is used in the code.
    E.g.: Two classes, A and B (extends A). If B is referenced directly in the code, it is available at compile time, i.e., A a = new B();

What does it mean by saying "not available at compile time"?

  • The compile time class and runtime class are different, i.e., for example base class is loaded using classname of child class for example Class.forName("classname") E.g.: Two classes, A and B (extends A). Code has
    A a = Class.forName("B").newInstance();

Solution 3:

NoClassDefFoundError means that the class is present in the classpath at Compile time, but it doesn't exist in the classpath at Runtime.

If you're using Eclipse, make sure you have the shapes, linepoints and the spaceobjects as entries in the .classpath file.

Solution 4:

If you got one of these errors while compiling and running:

  • NoClassDefFoundError

  • Error: Could not find or load main class hello

  • Exception in thread "main" java.lang.NoClassDefFoundError:javaTest/test/hello (wrong name: test/hello)

    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    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 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    

-------------------------- Solution -----------------------

The problem is mostly in packages organization. You should arrange your classes in folders properly regarding to the package classifications in your source code.

On compiling process, use this command:

javac -d . [FileName.java]

To run the class, please use this command:

java [Package].[ClassName]