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.
Below image shows NoClassDefFoundError
class diagram and it’s super classes. As you can see that it’s super classes are Throwable
and Error
.
Let’s first try to replicate a scenario where we get NoClassDefFoundError at runtime. Let’s say we have a java classes like below.
public class Data {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
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.
public class DataTest {
public static void main(String[] args) {
Data data = new Data();
data.setId(10);
System.out.println("Data Id = "+data.getId());
}
}
Now let’s compile DataTest class and then execute it like below.
pankaj:temp pankaj$ ls
Data.java DataTest.java
pankaj:temp pankaj$ javac DataTest.java
pankaj:temp pankaj$ ls
Data.class Data.java DataTest.class DataTest.java
pankaj:temp pankaj$ java DataTest
Data Id = 10
pankaj:temp pankaj$
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.
pankaj:temp pankaj$ mv Data.java Data.class ../
pankaj:temp pankaj$ ls
DataTest.class DataTest.java
pankaj:temp pankaj$ java DataTest
Exception in thread "main" java.lang.NoClassDefFoundError: Data
at DataTest.main(DataTest.java:5)
Caused by: java.lang.ClassNotFoundException: Data
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
pankaj:temp pankaj$
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.
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.
pankaj:temp pankaj$ java -classpath .:.. DataTest
Data Id = 10
pankaj:temp
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.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
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
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
How can i fix this on my Samsung Galaxy Prime phone?
- Moira