当前位置:网站首页>ClassNotFoundException v/s NoClassDefFoundError
ClassNotFoundException v/s NoClassDefFoundError
2022-06-24 16:44:00 【Erossssssss】
In this section , Let's talk about ClassNotFoundException And NoClassDefFoundError The difference between .
Usually, both of these cases are caused by the lack of necessary .class file , But there are differences between them .
ClassNotFoundException | NoClassDefFoundError |
|---|---|
This usually happens when the program is executing , Use class load Statement load missing .class file :Class.forName(“class.name”);ClassLoader.loadClass(“class.name”);ClassLoader.findSystemClass(“class.name”); reason : Because the runtime did not update JAR File and execute the program , Missing required file in classpath . | The two errors are similar , But the difference is that it is necessary .class Files are available at compile time , Lost at runtime . Probable cause : It is deleted after compilation. There may be a version mismatch |
The fully qualified class name is java.lang.ClassNotFoundException | The fully qualified class name is java.lang.NoClassDefoundError |
It belongs to the exception category , namely java.lang.Exception The direct subclass of . | It belongs to the error category , namely java.lang.Error, java.lang.LinkageError Subclasses of . |
It's a Checked Exception, So whenever you encounter something like 1 Click the class loading statement described above , Need to deal with it . | all Error All belong to unchecked Exception Category , therefore NoClassDeffoundError Also belong to unchecked Exception. |
When checking for exceptions , Programmers can use try catch Block or declaration throws Clause to provide processing code . therefore , It can be solved . | Program execution Java Runtime System The resulting error is therefore , It is difficult to reproduce . |
Example 1 | Example 2 |
- Class.forName(“class.name”);
- ClassLoader.loadClass(“class.name”);
- ClassLoader.findSystemClass(“class.name”);
reason : Due to runtime Not updated JAR file And execute the program , Missing required file in classpath . The two errors are similar , But the difference is that it is necessary .class Files are available at compile time , Lost at runtime . Probable cause :
- It is deleted after compilation
- Possible Version mismatch
The fully qualified class name is java.lang.ClassNotFoundException The fully qualified class name is java.lang.NoClassDefoundError It belongs to the exception category , namely java.lang.Exception The direct subclass of . It belongs to the error category , namely java.lang.Error, java.lang.LinkageError Subclasses of . It's a Checked Exception, So whenever you encounter something like 1 Click the class loading statement described above , Need to deal with it . all Error All belong to unchecked Exception Category , therefore NoClassDeffoundError Also belong to unchecked Exception. When checking for exceptions , Programmers can use try catch Block or declaration throws Clause to provide processing code . therefore , It can be solved . Program execution Java Runtime System Error caused therefore , It is difficult to reproduce . Example 1 Example 2
Example 1:ClassNotFoundException Demo example on :
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 results :
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) |
|---|
In the example above , We are trying to use Class.forName() Static method loading Oracle Driver file for database , But it is not available at runtime . Possible causes of such anomalies , perform JDBC Program without using the required JAR File update classpath , Solution : To correct this anomaly , Just put the necessary ojdbc14.jar Included in the classpath , Then perform the same procedure .
Example 2:NoClassDefoundError A demo example of :
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"); } } |
|---|
Running results :
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" |
|---|
In the example above , After compiling, we deliberately delete testfile.class To display this exception type . We are trying to execute a program , however classpath Missing required class file in TestFile.class. Possible causes of this exception type , A required file exists during compilation , But the file was lost while executing the same program The above program shows “has-a” Relationship , Compile successfully , And during the execution of the program ,JVM Can't find what you need .class file .
边栏推荐
- 6 things all engineers should know before FEA
- What is thermal data detection?
- Batch BOM Bapi test
- proxy pattern
- There are potential safety hazards Land Rover recalls some hybrid vehicles
- Modern finite element analysis can easily achieve accurate results
- Yuanqi forest started from 0 sugar and fell at 0 sugar
- What is a reptile
- Advanced programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization
- Istio FAQ: sidecar startup sequence
猜你喜欢

Problems encountered in the work of product manager

A survey on dynamic neural networks for natural language processing, University of California

C. K-th not divisible by n (Mathematics + thinking) codeforces round 640 (Div. 4)

There are potential safety hazards Land Rover recalls some hybrid vehicles

A survey on model compression for natural language processing (NLP model compression overview)
![[go] concurrent programming channel](/img/6a/d62678467bbc6dfb6a50ae42bacc96.jpg)
[go] concurrent programming channel

Cognition and difference of service number, subscription number, applet and enterprise number (enterprise wechat)

Applet - use of template

Some adventurer hybrid versions with potential safety hazards will be recalled

A survey of training on graphs: taxonomy, methods, and Applications
随机推荐
Nonholonomic constrained robot
The RTSP video structured intelligent analysis platform easynvr stops calling the PTZ interface through the onvif protocol to troubleshoot the pending status
A troubleshooting of golang memory leak
Saying "Dharma" Today: the little "secret" of paramter and localparam
Transpose convolution learning notes
Annual contribution! Tencent cloud middleware product upgrade conference is in hot registration!
What is the difference between a network card and a port
Little red book, hovering on the edge of listing
[idea] dynamic planning (DP)
What can Lu yuanjiu Jiao buy?
Modern finite element analysis can easily achieve accurate results
Automatically refresh CDN cache in real time based on cos log
Principle analysis of robot hardware in the loop system
proxy pattern
Don't let [mana] destroy your code!
Bypass kernel function pointer integrity check
Heavy release! Tencent cloud ASW workflow, visual orchestration cloud service
Interpretation of swin transformer source code
Tencent releases the full platform version of reasoning framework TNN, and supports mobile terminal, desktop terminal and server terminal at the same time
Talk about some good ways to participate in the project