W3docs

Why am I getting a NoClassDefFoundError in Java?

A NoClassDefFoundError in Java indicates that the Java Virtual Machine (JVM) or a ClassLoader was not able to find the definition of a class that was referenced in your code. This can occur for a variety of reasons, including:

A NoClassDefFoundError in Java indicates that the Java Virtual Machine (JVM) or a ClassLoader was not able to find the definition of a class that was referenced in your code. This can occur for a variety of reasons, including:

  • The class was not included on the classpath when the JVM was started.
  • The class was present on the classpath when the JVM was started, but it was subsequently removed or modified.
  • The class is a part of a library or a plugin, and the library or plugin is not installed or not available.

Difference from ClassNotFoundException NoClassDefFoundError is an Error thrown by the JVM during class linking or initialization when a class was available at compile time but is missing at runtime. In contrast, ClassNotFoundException is a checked exception thrown explicitly by reflection APIs (like Class.forName() or ClassLoader.loadClass()) when a class cannot be found dynamically.

Sample Stack Trace

Exception in thread "main" java.lang.NoClassDefFoundError: com/example/MyHelper
    at com.example.Main.run(Main.java:10)
    at com.example.Main.main(Main.java:5)
Caused by: java.lang.ClassNotFoundException: com.example.MyHelper
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    ...

Verifying the Classpath To troubleshoot a NoClassDefFoundError, you should first check that the class is present on the classpath. You can verify this using command-line tools or IDE settings:

  • Command Line: Run java -verbose:class YourMainClass to see which classes are loaded. To inspect a JAR, use jar tf your-library.jar | grep com/example/MyHelper.
  • IDE: In IntelliJ IDEA or Eclipse, verify that the missing class or its JAR is included in the project's Build Path / Module Dependencies.

If the class is present and the error persists, you should check that the class was not modified or deleted after the JVM was started.

If the class is a part of a library or a plugin, you should check that the library or plugin is installed and available to the JVM. You should also make sure that any dependencies of the library or plugin are also installed and available.

If you are unable to resolve the NoClassDefFoundError, you may need to provide more information about your code, the classpath, and the environment in which the error is occurring.