How to fix NoClassDefFoundError in Java? [Answered]

Problem:

I am getting a NoClassDefFoundError when I run my Java application. What is typically the cause of this?

NoClassDefFoundError in Java- Answer #1:

While it’s possible that this is due to a classpath mismatch between compile-time and run-time, it’s not necessarily true.

It is important to keep two or three different exceptions straight in our head in this case:

  1. java.lang.ClassNotFoundException This exception indicates that the class was not found on the classpath. This indicates that we were trying to load the class definition, and the class did not exist on the classpath.
  2. java.lang.NoClassDefFoundError This exception indicates that the JVM looked in its internal class definition data structure for the definition of a class and did not find it. This is different than saying that it could not be loaded from the classpath. Usually this indicates that we previously attempted to load a class from the classpath, but it failed for some reason – now we’re trying to use the class again (and thus need to load it, since it failed last time), but we’re not even going to try to load it, because we failed loading it earlier (and reasonably suspect that we would fail again). The earlier failure could be a ClassNotFoundException or an ExceptionInInitializerError (indicating a failure in the static initialization block) or any number of other problems. The point is, a NoClassDefFoundError is not necessarily a classpath problem.

Answer #2:

This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpath.

Answer #3:

Here is the code to illustrate java.lang.NoClassDefFoundError. Please see the first answer for a detailed explanation.

NoClassDefFoundErrorDemo.java

public class NoClassDefFoundErrorDemo {
    public static void main(String[] args) {
        try {
            // The following line would throw ExceptionInInitializerError
            SimpleCalculator calculator1 = new SimpleCalculator();
        } catch (Throwable t) {
            System.out.println(t);
        }
        // The following line would cause NoClassDefFoundError
        SimpleCalculator calculator2 = new SimpleCalculator();
    }

}

SimpleCalculator.java

public class SimpleCalculator {
    static int undefined = 1 / 0;
}

Answer #4:

NoClassDefFoundError In Java

Definition:

  1. Java Virtual Machine is not able to find a particular class at runtime which was available at compile time.
  2. If a class was present during compile time but not available in java classpath during runtime.
enter image description here

Examples:

  1. The class is not in Classpath, there is no sure shot way of knowing it but many times you can just have a look to print System.getproperty(“java.classpath”) and it will print the classpath from there you can at least get an idea of your actual runtime classpath.
  2. A simple example of NoClassDefFoundError is class belongs to a missing JAR file or JAR was not added into classpath or sometimes jar’s name has been changed by someone like in my case one of my colleagues has changed tibco.jar into tibco_v3.jar and the program is failing with java.lang.NoClassDefFoundError and I were wondering what’s wrong.
  3. Just try to run with explicitly -classpath option with the classpath you think will work and if it’s working then it’s a sure short sign that someone is overriding java classpath.
  4. Permission issue on JAR file can also cause NoClassDefFoundError in Java.
  5. Typo on XML Configuration can also cause NoClassDefFoundError in Java.
  6. when your compiled class which is defined in a package, doesn’t present in the same package while loading like in the case of JApplet it will throw NoClassDefFoundError in Java.

Possible Solutions:

  1. The class is not available in Java Classpath.
  2. If you are working in J2EE environment than the visibility of Class among multiple Classloader can also cause java.lang.NoClassDefFoundError, see examples and scenario section for detailed discussion.
  3. Check for java.lang.ExceptionInInitializerError in your log file. NoClassDefFoundError due to the failure of static initialization is quite common.
  4. Because NoClassDefFoundError is a subclass of java.lang.LinkageError it can also come if one of it dependency like native library may not available.
  5. Any start-up script is overriding Classpath environment variable.
  6. You might be running your program using jar command and class was not defined in manifest file’s ClassPath attribute.

Answer #5:

I have found that sometimes I get a NoClassDefFound error when code is compiled with an incompatible version of the class found at runtime. The specific instance I recall is with the apache axis library. There were actually 2 versions on my runtime classpath and it was picking up the out of date and incompatible version and not the correct one, causing a NoClassDefFound error. This was in a command line app where I was using a command similar to this.

set classpath=%classpath%;axis.jar

I was able to get it to pick up the proper version by using:

set classpath=axis.jar;%classpath%;

Answer #6:

This is the best solution I found so far.

Suppose we have a package called org.mypackage containing the classes:

  • HelloWorld (main class)
  • SupportClass
  • UtilClass

and the files defining this package are stored physically under the directory D:\myprogram (on Windows) or /home/user/myprogram (on Linux).

The file structure will look like this: 

When we invoke Java, we specify the name of the application to run: org.mypackage.HelloWorld. However, we must also tell Java where to look for the files and directories defining our package. So to launch the program, we have to use the following command:

Answer #7:

I was using Spring Framework with Maven and solved this error in my project.

There was a runtime error in the class. I was reading a property as integer, but when it read the value from the property file, its value was double.

Spring did not give me a full stack trace of on which line the runtime failed. It simply said NoClassDefFoundError. But when I executed it as a native Java application (taking it out of MVC), it gave ExceptionInInitializerError which was the true cause and which is how I traced the error.

Answer #8:

One interesting case in which you might see a lot of NoClassDefFoundErrors is when you:

  1. throw a RuntimeException in the static block of your class Example
  2. Intercept it (or if it just doesn’t matter like it is thrown in a test case)
  3. Try to create an instance of this class Example
static class Example {
    static {
        thisThrowsRuntimeException();
    }
}

static class OuterClazz {

    OuterClazz() {
        try {
            new Example();
        } catch (Throwable ignored) { //simulating catching RuntimeException from static block
            // DO NOT DO THIS IN PRODUCTION CODE, THIS IS JUST AN EXAMPLE in StackOverflow
        }

        new Example(); //this throws NoClassDefFoundError
    }
}

NoClassDefError will be thrown accompanied with ExceptionInInitializerError from the static block RuntimeException.


This is especially important case when you see NoClassDefFoundErrors in your UNIT TESTS.

In a way you’re “sharing” the static block execution between tests, but the initial ExceptionInInitializerError will be just in one test case. The first one that uses the problematic Example class. Other test cases that use the Example class will just throw NoClassDefFoundErrors.

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →