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();
}
}
Compile both classes successfully.
Now delete Employee.class and run:
Exception in thread "main"
java.lang.NoClassDefFoundError: Employee
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
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;
}
}
public class Demo {
public static void main(String[] args) {
Employee emp = new Employee();
}
}
Output:
Exception in thread "main"
java.lang.ExceptionInInitializerError
If the JVM later tries to use Employee again:
java.lang.NoClassDefFoundError:
Could not initialize class Employee
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();
}
}
If Employee.class is missing:
Exception in thread "main"
java.lang.ClassNotFoundException: Employee
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);
The compiler requires:
try {
Class.forName("Employee");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
NoClassDefFoundError (Unchecked)
With normal Java code:
Employee emp = new Employee();
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");
If the MySQL driver JAR is missing:
ClassNotFoundException
NoClassDefFoundError
- Missing dependency JAR
- Deleted
.classfile - 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(...)
NoClassDefFoundError → "The JVM expected the class to already be available."
Think:
new Employee()
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)
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)