Understanding ClassNotFoundException vs NoClassDefFoundError in Java
This article explains the difference between Java's ClassNotFoundException and NoClassDefFoundError, describing their inheritance, when each is thrown by the JVM, how they affect program execution, and provides code examples that illustrate their distinct behaviors.
When a Java program cannot locate a class, the JVM may throw either ClassNotFoundException or NoClassDefFoundError ; although both indicate a missing class, they represent different situations and have different handling requirements.
ClassNotFoundException
ClassNotFoundException is a checked exception that extends Exception . It is thrown when a class loader attempts to load a class (e.g., via Class.forName , ClassLoader.loadClass , or ClassLoader.findSystemClass ) and the class cannot be found on the classpath.
A common scenario is loading a JDBC driver with Class.forName . If the driver JAR is not on the classpath, the call results in a ClassNotFoundException .
public class MainClass {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}Running the above code without the Oracle driver on the classpath produces output similar to:
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
...NoClassDefFoundError
NoClassDefFoundError is an Error (subclass of Throwable ) and therefore is unchecked; the application is not required to catch it. It occurs when a class was available at compile time but cannot be found at runtime, typically after the class file has been removed or not included in the runtime classpath.
For example, if a helper class TempClass is compiled, then its .class file is deleted before the program runs, the JVM throws NoClassDefFoundError when the program tries to instantiate it.
public class TempClass {
}
public class MainClass {
public static void main(String[] args) {
TempClass t = new TempClass();
}
}After deleting TempClass.class and executing MainClass , the output is:
Exception in thread "main" java.lang.NoClassDefFoundError: TempClass
at MainClass.main(MainClass.java:6)
Caused by: java.lang.ClassNotFoundException: TempClass
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
...Summary
ClassNotFoundException is a checked exception indicating that the JVM could not locate a class during a dynamic loading operation, while NoClassDefFoundError is an unchecked error that signals a class present at compile time is missing at runtime; understanding both helps developers diagnose class‑loading problems more effectively.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.