Blog |

How to Resolve the NoClassDefFoundError in Java

How to Resolve the NoClassDefFoundError in Java
Table of Contents

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.

 

What Causes NoClassDefFoundError

The NoClassDefFoundError occurs in Java when the JVM is unable to find a particular class at runtime which was available at compile-time.

The definition of the class is attempted to be loaded as part of a normal method call or creating an instance of the class using the new expression and no definition of the class could be found. Therefore, it can occur during the linking or loading of the unavailable class.

Common causes of the class definition being unavailable at runtime are:

  • Missing JAR file
  • Permission issues
  • Incorrect classpath at runtime

 

NoClassDefFoundError Example

Here’s an example of a NoClassDefFoundError thrown when a class is attempted to be loaded that is available at compile-time but not at runtime:

class Vehicle {
    private String make;

    public String getMake() {
        return make;
    }

    public void setMake(String make) {
        this.make = make;
    }
}

public class NoClassDefFoundErrorExample {
    public static void main(String args[]) {
        Vehicle vehicle = new Vehicle();
        vehicle.setMake("Audi");
        System.out.println("Make = " + vehicle.getMake());
    }
}

In the above example, an instance of the Vehicle class is created in the NoClassDefFoundErrorExample.main() method and one of its methods is called. When the NoClassDefFoundErrorExample class is compiled and executed using the command line, it works fine and produces the correct output as expected:

$ ls
NoClassDefFoundErrorExample.class   Vehicle.class
NoClassDefFoundErrorExample.java
$ javac NoClassDefFoundErrorExample.java 
$ java NoClassDefFoundErrorExample
Make = Audi

Now, if the Vehicle.class file is renamed and the NoClassDefFoundErrorExample class is executed again without recompiling, the NoClassDefFoundError is thrown:

$ mv Vehicle.class Vehicle2.class 
$ ls
NoClassDefFoundErrorExample.class   Vehicle2.class
NoClassDefFoundErrorExample.java
$ java NoClassDefFoundErrorExample
Exception in thread "main" java.lang.NoClassDefFoundError: Vehicle
    at NoClassDefFoundErrorExample.main(NoClassDefFoundErrorExample.java:15)
Caused by: java.lang.ClassNotFoundException: Vehicle
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 1 more

 

How to Resolve NoClassDefFoundError

The following steps should be followed to resolve a NoClassDefFoundError in Java:

  • The most common reason for the NoClassDefFoundError is that a particular class is not available in the application classpath. Find out which JAR file contains the problematic class and check whether this JAR is present in the application classpath. If not, the JAR should be added to the classpath and the application should be recompiled and executed again.
  • If that JAR is already present in the classpath, make sure the classpath is not overridden (e.g. by a start-up script). After finding out the exact classpath used by the application, the JAR file should be added to it.
  • Check the manifest file to see if the unavailable class is not defined in the Class-Path attribute. If so, it should be defined.
  • The NoClassDefFoundError can also occur due to the failure of static initialization. Check for the java.lang.ExceptionInInitializerError in the application logs.

 

Track, Analyze and Manage Errors With Rollbar

Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!

Related Resources

"Rollbar allows us to go from alerting to impact analysis and resolution in a matter of minutes. Without it we would be flying blind."

Error Monitoring

Start continuously improving your code today.

Get Started Shape