Tutorial

java.lang.NoClassDefFoundError

Published on August 3, 2022
Default avatar

By Pankaj

java.lang.NoClassDefFoundError

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.

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
October 12, 2019

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

- Moira

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel