当前位置:网站首页>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 .
边栏推荐
- ThinkPHP vulnerability exploitation tool
- Transpose convolution learning notes
- Pytorch transpose convolution
- [tke] enable CPU static management strategy
- Modern finite element analysis can easily achieve accurate results
- Fastjson vulnerability utilization techniques
- One article combs multi task learning (mmoe/ple/dupn/essm, etc.)
- Detailed explanation of transpose convolution in pytorch
- Leetcode notes of Google boss | necessary for school recruitment!
- How FEA and FEM work together
猜你喜欢

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

A survey of training on graphs: taxonomy, methods, and Applications

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

Ui- first lesson
Advanced programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization
MySQL Advanced Series: locks - locks in InnoDB
![[go] concurrent programming channel](/img/6a/d62678467bbc6dfb6a50ae42bacc96.jpg)
[go] concurrent programming channel

A survey on model compression for natural language processing (NLP model compression overview)

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

Applet wxss
随机推荐
How do HPE servers make RAID5 arrays? Teach you step by step today!
Is Shanjin futures safe? What are the procedures for opening futures accounts? How to reduce the futures commission?
What is the difference between a network card and a port
[go] concurrent programming channel
50 growers | closed door meeting of marketing circle of friends ス gathering Magic City thinking collision to help enterprise marketing growth
Nonholonomic constrained robot
Druid architecture and Implementation
A memory leak caused by timeout scheduling of context and goroutine implementation
Prometheus deployment
Ui- first lesson
Ps\ai and other design software pondering notes
Talk about some good ways to participate in the project
What is Ethernet
MySQL Advanced Series: locks - locks in InnoDB
Object store signature generation
What is browser fingerprint recognition?
Kubernetes popular series: getting started with container Foundation
In those years, I insisted on learning the motivation of programming
Don't let [mana] destroy your code!
Annual contribution! Tencent cloud middleware product upgrade conference is in hot registration!