ClassNotFoundException VS NoClassDefFoundError In Java

ClassNotFoundException vs NoClassDefFoundError

In this article, we will discuss difference between ClassNotFoundException and NoClassDefFoundError in detail i.e.; ClassNotFoundException vs NoClassDefFoundError.

Often both occur due to an absence of required .class files during program execution, but there are differences between them. Lets us move on and discuss key differences between these ClassNotFoundException & NoClassDefFoundError exception.

ClassNotFoundException
NoClassDefFoundError
This generally occurs, when required .class is missing when the program encounters a class load statement such as,

 

  • Class.forName(“class.name”);
  • ClassLoader.loadClass(“class.name”);
  • ClassLoader.findSystemClass(“class.name”);

Reason: a required file missing in the classpath due to the execution of the program without updating JAR file at runtime

This is very much similar, but the difference is required .class file is available during compile-time & missing at runtime possible Reason:

 

  • It is deleted after compilation or
  • there could be a version mismatch
The fully qualified class name is java.lang.ClassNotFoundExceptionThe fully qualified class name is java.lang.NoClassDefFoundError
It falls under the category of Exception, i.e., a direct subclass of java.lang.ExceptionIt falls under the category of Error, i.e., sub-class of java.lang.Error through java.lang.LinkageError
It is a checked exception. Therefore it needs to be handled, whenever class loading statement is encountered as stated in point no.1All errors come under unchecked exception category. Therefore NoClassDefFoundError is also an unchecked exception
As it is checked exception, the programmer can provide handling code either using the try-catch block or can declare throws clauseTherefore, and it is recoverableErrors are thrown by Java Runtime system during program execution. Therefore, it is non-recoverable
Check Example 1Check Example 2

Example 1: Demo example on ClassNotFoundException:

JdbcConnectionExample.java

package in.bench.resources.top.exception.in.java;
public class JdbcConnectionExample {
    public static void main(String[] args) {
        // declare variables
        // Step 1: Loading or registering Oracle JDBC driver class
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        }
        catch(ClassNotFoundException cnfex) {
            System.out.println("Problem in loading Oracle JDBC driver");
            cnfex.printStackTrace();
        }
        // Step 2: Opening database connection
    }
}

Output:

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
Problem in loading Oracle JDBC driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at in.bench.resources.top.exception.in.java.JdbcConnectionExample
.main(JdbcConnectionExample.java:11)

Explanation:

In the above example,

  • we are trying to load driver file for Oracle databases using forName() method of a Class class, but it isn’t available at runtime
  • A possible reason for this type of exception is, executing a JDBC program without updating class-path with required JAR files
  • Solution: to rectify this exception, include the required ojdbc14.jar into the class-path and then again execute the same program

Example 2: Demo example on NoClassDefFoundError:

SimilarException.java

package in.bench.resources.top.exception.in.java;
public class SimilarException {
    // using below declared TestFile class
    static TestFile tf = new TestFile();
    public static void main(String[] args) {
        // invoke method
        tf.display();
    }
}
class TestFile {
    public void display() {
        System.out.println("Display message");
    }
}

Output:

java.lang.NoClassDefFoundError: in/bench/resources/top/exception/in/java/TestFile
    at in.bench.resources.top.exception.in.java.SimilarException.<clinit>(SimilarException.java:6)
Caused by: java.lang.ClassNotFoundException: in.bench.resources.top.exception.in.java.TestFile
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more
Exception in thread "main"

Explanation:

In the above example,

  • We are trying to execute a program, and required .class files are missing from class-path
  • A possible reason for this exception-type is, the required file is present during compilation but missing while executing the same program
  • Above program exhibits “HAS-A” relationship & compilation succeeds whereas during program execution JVM unable to find a required .class file
  • Note: deliberately deleted TestFile.class after compilation to showcase this exception-type

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment