DEV Community

Rajesh Bhola
Rajesh Bhola

Posted on

ClassNotFoundException vs NoClassDefFoundError

This is one of the most frequently asked Java interview questions.

Although both occur when a class cannot be loaded, they happen for different reasons.


Quick Definition

Item ClassNotFoundException NoClassDefFoundError
Type Checked Exception Unchecked Error
Caused By JVM cannot find a class requested dynamically JVM cannot find a class that was available during compilation

Side-by-Side Comparison

Property ClassNotFoundException NoClassDefFoundError
Type Checked Exception Error (Unchecked)
Hierarchy Exception → ReflectiveOperationException → ClassNotFoundException Throwable → Error → LinkageError → NoClassDefFoundError
Trigger Dynamic loading (Class.forName(), ClassLoader.loadClass()) Normal class loading (new, static references, inheritance, etc.)
Compile-time handling ✅ Must handle (try-catch or throws) ❌ No handling required
Recoverable Usually yes Usually no
Primary Cause Class name supplied at runtime cannot be found Class existed during compilation but is unavailable or failed initialization at runtime

Example 1: NoClassDefFoundError (Normal Class Loading)

class Employee {
}

public class Demo {

    public static void main(String[] args) {
        Employee emp = new Employee();
    }

}
Enter fullscreen mode Exit fullscreen mode

Compile both classes successfully.

Now delete Employee.class and run:

Exception in thread "main"
java.lang.NoClassDefFoundError: Employee
Enter fullscreen mode Exit fullscreen mode

Why?

The compiler successfully compiled the program because Employee.class existed during compilation.

At runtime, the JVM tried to load Employee.class but couldn't find it.

Hence,

NoClassDefFoundError
Enter fullscreen mode Exit fullscreen mode

Another Common Cause

NoClassDefFoundError is not only caused by missing .class files.

It also occurs when class initialization fails.

Example:

class Employee {

    static {
        int x = 10 / 0;
    }

}
Enter fullscreen mode Exit fullscreen mode
public class Demo {

    public static void main(String[] args) {
        Employee emp = new Employee();
    }

}
Enter fullscreen mode Exit fullscreen mode

Output:

Exception in thread "main"
java.lang.ExceptionInInitializerError
Enter fullscreen mode Exit fullscreen mode

If the JVM later tries to use Employee again:

java.lang.NoClassDefFoundError:
Could not initialize class Employee
Enter fullscreen mode Exit fullscreen mode

This is an important interview point that many candidates miss.


Example 2: ClassNotFoundException (Dynamic Loading)

public class Demo {

    public static void main(String[] args)
            throws Exception {

        Class<?> cls =
                Class.forName("Employee");

        Object obj =
                cls.getDeclaredConstructor()
                   .newInstance();
    }

}
Enter fullscreen mode Exit fullscreen mode

If Employee.class is missing:

Exception in thread "main"
java.lang.ClassNotFoundException: Employee
Enter fullscreen mode Exit fullscreen mode

Why?

Class.forName() loads a class using its name provided at runtime.

Since the compiler cannot verify whether the class will exist, Java forces you to handle the possibility using a checked exception.


Why is One Checked and the Other Unchecked?

ClassNotFoundException (Checked)

The compiler knows you're trying to load a class dynamically.

Since the class name comes from runtime information (configuration, database, user input, etc.), it may not exist.

Therefore Java forces you to handle it.

Example:

Class.forName(className);
Enter fullscreen mode Exit fullscreen mode

The compiler requires:

try {
    Class.forName("Employee");
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}
Enter fullscreen mode Exit fullscreen mode

NoClassDefFoundError (Unchecked)

With normal Java code:

Employee emp = new Employee();
Enter fullscreen mode Exit fullscreen mode

the compiler has already verified that Employee.class exists.

If it disappears later, that's usually a deployment, classpath, packaging, or initialization problem—not something normal application code is expected to recover from.

Therefore Java treats it as an Error.


Real-World Causes

ClassNotFoundException

  • Loading JDBC drivers dynamically
  • Reflection
  • Plugin systems
  • Application servers
  • Custom ClassLoaders
  • Reading class names from configuration files

Example:

Class.forName("com.mysql.cj.jdbc.Driver");
Enter fullscreen mode Exit fullscreen mode

If the MySQL driver JAR is missing:

ClassNotFoundException
Enter fullscreen mode Exit fullscreen mode

NoClassDefFoundError

  • Missing dependency JAR
  • Deleted .class file
  • Incorrect deployment
  • Wrong classpath
  • Failed static initialization
  • Version mismatch between libraries

Memory Trick 🧠

ClassNotFoundException → "I asked the JVM to find a class by name."

Think:

Class.forName(...)
Enter fullscreen mode Exit fullscreen mode

NoClassDefFoundError → "The JVM expected the class to already be available."

Think:

new Employee()
Enter fullscreen mode Exit fullscreen mode

Quick Interview Table

Code Exception/Error
new Employee() NoClassDefFoundError
Class.forName("Employee") ClassNotFoundException
Missing dependency JAR during reflection ClassNotFoundException
Missing dependency JAR during normal object creation NoClassDefFoundError
Static initializer fails (static { ... }) First: ExceptionInInitializerError, Later: NoClassDefFoundError

Visual Summary

                   Class required at runtime
                            │
               ┌────────────┴────────────┐
               │                         │
               ▼                         ▼
      Loaded dynamically?         Loaded normally?
      (Class.forName())             (new Employee())
               │                         │
               ▼                         ▼
     ClassNotFoundException      NoClassDefFoundError
       (Checked Exception)         (Unchecked Error)
Enter fullscreen mode Exit fullscreen mode

If you found this guide helpful, leave a ❤️ and follow for more beginner-friendly Java tutorials, interview questions, and practical coding examples.

Happy Coding!

Top comments (0)