java.lang.NoClassDefFoundError

Filed Under: Java

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.

java.lang.NoClassDefFoundError

java.lang.NoClassDefFoundError

  • 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.

java lang NoClassDefFoundError Class Diagram

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.


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.

java.lang.NoClassDefFoundError example

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.
    
    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

Comments

  1. Moira says:

    How can i fix this on my Samsung Galaxy Prime phone?

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages