Difference between ClassNotFoundException v/s NoClassDefFoundError

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

ClassNotFoundException v/s NoClassDefFoundError

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

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

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:

  • It is deleted after compilation or
  • there could be version mismatch
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 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() 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

 

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 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

 

References:

 

Happy Coding !!
Happy Learning !!

Interview question and answer on Exception Handling in Java
Top Exception and Error in Java