Basic investigation: What is the difference between ClassNotFoundException and NoClassDefFoundError

When writing Java programs, when a class cannot be found, the JVM sometimes throws ClassNotFoundException and sometimes NoClassDefFoundError. Looking at the literal meaning of the two exceptions, it seems that the class cannot be found, but why does the JVM use two exceptions to distinguish the situation where the class cannot be found? What is the difference between these two exceptions?

ClassNotFoundException

ClassNotFoundException is a runtime exception. From the perspective of class inheritance, ClassNotFoundException is inherited from Exception, so ClassNotFoundException is a checked exception.

image.png

When the application is running and trying to use the class loader to load the class file, if the specified class is not found in the classpath, a ClassNotFoundException will be thrown. In general, when we use Class.forName() or ClassLoader.loadClass and use ClassLoader.findSystemClass() to load a class at runtime, if the class is not found, it will cause the JVM to throw a ClassNotFoundException.

The simplest, when we use JDBC to connect to the database, we generally use Class.forName() to load the JDBC driver. If we do not put the driver in the application's classpath, it will cause the runtime to fail to find it. to the class, so running Class.forName() throws a ClassNotFoundException.

public class MainClass 
{    
public static void main(String[] args) 
{        
try {            
Class.forName("oracle.jdbc.driver.OracleDriver");        
} 
catch (ClassNotFoundException e) 
{            
e.printStackTrace();        
}    
}
}

output:

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver    
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)    
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)    
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)    
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)    
at java.lang.Class.forName0(Native Method)    
at java.lang.Class.forName(Class.java:264)    
at MainClass.main(MainClass.java:7)

NoClassDefFoundError

NoClassDefFoundError exception, see the naming suffix is ​​an Error. From the class inheritance level, NoClassDefFoundError is inherited from Error. One obvious difference from ClassNotFoundException is that NoClassDefFoundError does not require the application to care about the catch.

image.png

When the JVM loads a class, if the class is available at compile time, but cannot find the class definition at runtime, the JVM will throw a NoClassDefFoundError error. For example, when we new an instance of a class, if the class cannot be found at runtime, a NoClassDefFoundError error will be thrown.

public class TempClass {}public class MainClass 
{    
public static void main(String[] args) 
{        
TempClass t = new TempClass();    
}
}

First, we first create a TempClass, then after compiling, delete the TempClass.class file produced by TempClass, and then execute the program to output:

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)    
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)    
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)    
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)    ... 1 more

Summarize

image.png
[Here I want to say that because I have taken many detours to come here, I made up my mind to organize it. Although the collection process is not easy, it is sweet to think that it can help some self-learning java people! If you need a partner, please click the ㊦ square】↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

Guess you like

Origin blog.csdn.net/m0_54828003/article/details/127287291