In this article, we will discuss difference between ClassNotFoundException and NoClassDefFoundError in detail i.e.; ClassNotFoundException v/s NoClassDefFoundError
Before processing further read below articles,
Often both occurs due to absence of required .class files during program execution, but there are differences between them
1. ClassNotFoundException v/s NoClassDefFoundError
ClassNotFoundException | NoClassDefFoundError |
This is generally occurs, when required .class is missing when program encounters class load statement such as,
Reason: required file missing in the class-path due to execution of 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:
|
Fully qualified class name is java.lang.ClassNotFoundException | Fully qualified class name is java.lang.NoClassDefFoundError |
It falls under the category of Exception i.e.; direct sub-class of java.lang.Exception
| It 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.1 | All errors come under unchecked exception category, therefore NoClassDefFoundError is also unchecked exception |
As it is checked exception, programmer can provide handling code either using try-catch block or can declare throws clause
Therefore, it is recoverable | Errors are thrown by Java Runtime system during program execution
Therefore, it is non-recoverable |
Example 1.2 | Example 1.2 |
1.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() static-method of Class class, but it isn’t available at runtime
- Possible reason for this type of exception, executing JDBC program without updating class-path with required JAR files
- Solution: to rectify this exception, just include required ojdbc14.jar into class-path and then execute same program
1.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 is missing from class-path
- Possible reason for this exception-type, required file is present during compilation but missing while executing same program
- Above program exhibits “HAS-A” relationship & compilation succeeds whereas during program execution JVM unable to find required .class file
- Note: deliberately deleted TestFile.class after compilation to showcase this exception-type
Related Articles:
- Java – Exception Handling
- Java – Exception Hierarchy
- Java – 5 important keywords in Java Exception handling
- Java – Runtime mechanism, what happens when exception is thrown ?
- Java – Checked Exception v/s Unchecked Exception
- Java – Exception propagation
- Java – try-catch block
- Java – finally block
- Java – try with multiple catch blocks
- Java – Nested try-catch block
- Java – Returning value from method having try-catch-finally blocks
- Java – return statement with finally block
- Java – final v/s finally v/s finalize
- Java – Various methods to print exception information
- Java – throw keyword
- Java – throws keyword
- Java – throw v/s throws
- Java – Difference between throws clause and try-catch-finally block
- Java – Rules for Exception handling w.r.t Method Overriding
- Java – User-defined or Custom exception
- Java – Difference between ClassNotFoundException v/s NoClassDefFoundError
- Java – Top Exception and Error
- Java – Interview question and answers on Exception Handling
- Java 7 – try with resources
- Java 7 – multi-catch block
References:
- https://docs.oracle.com/javase/7/docs/api/java/lang/ClassNotFoundException.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/NoClassDefFoundError.html
Happy Coding !!
Happy Learning !!