Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ClassNotFoundException vs. NoClassDefFoundError

DZone's Guide to

ClassNotFoundException vs. NoClassDefFoundError

This guide to the similar-but-different ClassNotFoundException and NoClassDefFoundError explains their differences and how to resolve them.

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

We know that Java is an OOP language and that almost everything is an object in Java. And in order to create an object, we need a class.

While executing our program, whenever the JVM find a class, first it will try to load that class into memory if it has not done so already.

For example, if the JVM is executing the line of code below, before creating the object of the Employee class, the JVM will load this class into memory using a ClassLoader.

Employee emp = new Employee();


In the above example, the JVM will load the Employee class because it is present in the execution path — and the JVM wants to create an object of this class.

But we can also ask the JVM to just load a class through its string name using Class.forName() or ClassLoader.findSystemClass() or ClassLoader.loadClass() methods. For Example, the following line of code will only load the Employee class into memory and do nothing else.

Class.forName("Employee");


Both ClassNotFoundException and NoClassDefFoundError occur when a particular class is not found at runtime — but under different scenarios. And here in this article, we going to study these different scenarios.

ClassNotFoundException

ClassNotFoundException is a checked exception that occurs when we tell the JVM to load a class by its string name using the Class.forName() or ClassLoader.findSystemClass() or ClassLoader.loadClass() methods and the mentioned class is not found in the classpath.

Most of the time, this exception occurs when you try to run an application without updating the classpath with the required JAR files. For example, you may have seen this exception when doing the JDBC code to connect to your database, i.e.MySQL, but your classpath does not have the JAR for it.

If we compile the example below, the compiler will produce two class files Test.class and Person.class. And now, if we execute the program, it will successfully print Hello. But if we delete the Person.class file and try to execute the program again, we will receive a ClassNotFoundException.

public class Test {
    public static void main(String[] args) throws Exception {
        Class clazz = Class.forName("Person");
        Person person = (Person) clazz.newInstance();
        person.saySomething();
    }
}

class Person {
    void saySomething() {
        System.out.println("Hello");
    }
}


NoClassDefFoundError

NoClassDefFoundError is a subtype of java.lang.Error, and the Error class indicates an abnormal behavior that really should not happen with an application. Meanwhile, application developers should not try to catch it — it is there for JVM use only.

NoClassDefFoundError occurs when the JVM tries to load a particular class that is the part of your code execution (as part of a normal method call or as part of creating an instance using the new  keyword) and that class is not present in your classpath — but was present at compile time. In order to execute your program, you need to compile it, and if you are trying use a class that is not present, the compiler will raise a compilation error.

Similar to the above example, if we try to compile the program below, we will get two class files: Test.class and Employee.class. Upon execution, it will print Hello.

public class Test {
    public static void main(String[] args) throws Exception {
        Employee emp = new Employee();
        emp.saySomething();

    }
}

class Employee {
    void saySomething() {
        System.out.println("Hello");
    }
}


But if we delete Employee.class and try to execute the program, we will get a NoClassDefFoundError.

Exception in thread "main" java.lang.NoClassDefFoundError: Employee
 at Test.main(Test.java:9)
Caused by: java.lang.ClassNotFoundException: Employee
 at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
 at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
 ... 1 more


As you can see in above stack trace, NoClassDefFoundError is caused by ClassNotFoundException, because the JVM is not able to find the Employee 

class in the classpath.

Conclusion

Difference-Between-ClassNotFoundException-and-NoClassDefFoundError

You can find complete code on this GitHub repository, and please feel free to provide your valuable feedback.

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:
jvm ,java ,classnotfoundexception ,noclassdeffounderror ,tutorial

Published at DZone with permission of Naresh Joshi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}