java.lang.NoClassDefFoundError With Examples

java.lang.NoClassDefFoundError is runtime error thrown when a required class is not found in the classpath and hence JVM is unable to load it into memory.


  • NoClassDefFoundError is a runtime error, so it’s beyond our application scope to anticipate and recover from this.
  • java.lang.NoClassDefFoundError is a runtime error, it never comes in compile time.
  • It’s very easy to debug NoClassDefFoundError because it clearly says that JVM was unable to find the required class, so check classpath configurations to make sure required classes are not missed.

NoClassDefFoundError Class Diagram

Below image shows NoClassDefFoundError class diagram and it’s super classes.


As you can see that it’s super classes are Throwable and Error.

java.lang.NoClassDefFoundError Reasons

Let’s first try to replicate a scenario where we get NoClassDefFoundError at runtime. Let’s say we have a java classes like below.

Notice that above class doesn’t depend on any other custom java classes, it just uses java built-in classes. Let’s create another class that will use Data class in the same directory.

Now let’s compile DataTest class and then execute it like below.

So far everything is fine, now let’s move Data class files to somewhere else and then try to execute DataTest class. We will not compile it again since then it will give compilation error.

Here it is, we got NoClassDefFoundError because java runtime is unable to find Data class as clearly shown in the exception stack trace. Below image shows all the above commands and output in the terminal window.

How to resolve java.lang.NoClassDefFoundError?

From above example, we can clearly identify that the only reason for this error is that the required classes were available at compile time but not at runtime. You can fix NoClassDefFoundError error by checking following:

  • Check the exception stack trace to know exactly which class throw the error and which is the class not found by java.
  • Next step is to look for classpath configuration, sometimes we compile our classes in Eclipse or some other environment and run in some other environment and we can miss classpath configurations. For example, I can fix above issue easily by adding the directory which contains Data class to the classpath like below.

    Remember that earlier I had moved Data class to previous directory.
  • Most of the times, NoClassDefFoundError comes with applications running on some server as web application or web services, in that case check if the required jars are part of the WAR file or not. For example, below maven configuration will not package jar file when generating WAR file.

    But we need it for creating a servlet based web application, usually this jar is always part of Tomcat or any other application server.

That’s all for a quick look at java.lang.NoClassDefFoundError, I hope you find enough idea when this error comes and how to fix it easily.

Reference: API Doc, Exception Handling in Java

By admin

Leave a Reply

%d bloggers like this: