1.

Difference between a ClassNotFoundException and NoClassDefFoundError?

Answer»

ClassNotFoundException and NoClassDefFoundError both OCCUR when a class is not found during runtime.

ClassNotFoundException is an exception that is thrown when we try to load a class while EXECUTION of a Java program.

NoClassDefFoundError is an error which is thrown when a class marks it presence during COMPILE time but isn’t available during runtime.

Despite their similarity of a missing class during runtime, there are a quite a few differences between ClassNotFoundException and NoClassDefFoundError.

Basis
ClassNotFoundException
NoClassDefFoundError
Parent class
ClassNotFoundException is a child class of java.lang.Exception
NoClassDefFoundError is a child class of java.lang.Error
Occurrence
It occurs when an application tries to load a class during runtime which is not UPDATED in the CLASSPATH
It occurs when the system doesn’t find the class definition during runtime which was present during compile time.
Thrown by
It is thrown by the application (program). Some methods like Class.forName(), loadClass() and findSystemClass() cause this exception.
It is thrown by the Java Runtime System.
Condition
It occurs when the classpath is not updated in accordance to the Java Archive files
It occurs when class definition is missing during runtime.


Discussion

No Comment Found