当前位置:网站首页>ClassNotFoundException vs NoClassDefFoundError
ClassNotFoundException vs NoClassDefFoundError
2022-06-09 02:31:00 【茫茫人海一粒沙】
当在运行时找不到类时,会发生 ClassNotFoundException 和 NoClassDefFoundError 这两个异常。 它们与 Java 类路径有关。
1. ClassNotFoundException
当您尝试在运行时使用 Class.forName() 或 loadClass() 方法加载类并且在类路径中找不到请求的类时,会发生 ClassNotFoundException。 大多数情况下,当您尝试运行应用程序而不使用 JAR 文件更新类路径时,会发生此异常。 此异常是从 java.lang.Exception 类派生的已检查异常,您需要为其提供显式处理。 当您有两个类加载器并且 ClassLoader 尝试访问由 Java 中的另一个类加载器加载的类时,也会发生此异常。 您一定想知道 Java 中的类加载器到底是什么。 Java ClassLoader 是 Java 运行时环境的一部分,它在 JVM(Java 虚拟机)中动态加载 Java 类。 由于类加载器,Java 运行时系统不需要了解文件和文件系统。
例子
// Java Program to Illustrate ClassNotFoundException
public class GFG {
// Main driver method
public static void main(String args[])
{
// Try block to check for exceptions
try {
Class.forName("GeeksForGeeks");
}
// Catch block to handle exceptions
catch (ClassNotFoundException ex) {
// Displaying exceptions on console along with
// line number using printStackTrace() method
ex.printStackTrace();
}
}
}
结果:
java.lang.ClassNotFoundException: GeeksForGeeks
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 GFG.main(GFG.java:10)
2. NoClassDefFoundError
现在讨论下一个异常,即 NoClassDefFoundError 在编译期间存在类并且程序已成功编译和链接但在运行时不存在该类时发生。 这是一个源自 LinkageError 的错误。 当一个类对另一个类有一些依赖,而后一个类在前一个类编译后发生变化时,就会发生链接错误。 NoClassFoundError 是由于调用方法或从该类访问变量而隐式加载类的结果。 这个错误比较难调试,也很难找到这个错误发生的原因。 因此,在这种情况下,您应该始终检查依赖于此类的类。 为了说明,让我们首先为 java 程序创建任意两个类并将它们链接起来。
例子
// Java Program to Illustrate NoClassDefFoundError Exception
// Class 1
// Helper class
class GeeksForGeeks {
// Method
void greeting()
{
// Print statement whenever method is called
System.out.println("hello!");
}
}
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Creating object of class 1
// inside main() in class2
GeeksForGeeks geeks = new GeeksForGeeks();
// Calling method of above class
geeks.greeting();
}
}
结果
hello!
上面的程序将成功编译并生成两个类“GeeksForGeeks.class”和“GFG.class”。现在删除 GeeksForGeeks.class 文件并运行 GFG.class,当我们看到在 Java 运行时会抛出 NoClassDefFoundError 时。
结果:
Exception in thread "main" java.lang.NoClassDefFoundError: GeeksForGeeks
at GFG.main(GFG.java:24)
Caused by: java.lang.ClassNotFoundException: GeeksForGeeks
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参考资料
ClassNotFoundException Vs NoClassDefFoundError in Java - GeeksforGeeks
https://www.baeldung.com/java-classnotfoundexception-and-noclassdeffounderror
边栏推荐
- Range of acwing 789 numbers
- toggleRowSelection()失效的2个重要影响因素
- 动态规划/备忘录方法 n的k拆分 n的最大加数k拆分
- Modbus RTU communication routine between Delta Eh3 series PLC and thermostat
- 杰理之如何修改 ad_key 连接的引脚?【篇】
- S series · add data to the text file without adding duplicate values
- file_ get_ contonts[ZJCTF 2019]NiZhuanSiWei
- Kubernetes scheduling framework extension point
- export相关知识
- Database table cannot add content display modification cannot save prompt 1452 error
猜你喜欢

【HomeAssistant外网访问(cpolar)】

2022.6 download and installation tutorial of the latest version of mongodb

Implementation of UESTC daily report based on Selenium

Binary tree chain structure

Jedis tool class, adapting to a single redis and redis cluster

蓝桥杯_青蛙的约会_扩展欧几里得

Deux facteurs importants affectant la défaillance de togglerowselection ()

NFT chain game system development | defi+nft technology construction

"The original, skillful and vulgar skills of cloud native" -- composition of Volume I of the national new college entrance examination in 2022

Processes and threads
随机推荐
Go to MFC from Win32
动态规划/备忘录方法 n的k拆分 n的最大加数k拆分
Problems and solutions in using renrenfast
SQLite3 syntax (1)
Write cycle condition task
21. Class E power amplifier design of ads usage record (medium)
Should flying books be ecological? Analyze the first independent SaaS case of all in flybook
Analyze several interview questions: = = and = = =; Binding events; regular expression
马上消费:打击征信修复不遗余力,乱象根治呼唤社会合力
Immediate consumption: spare no effort to crack down on credit investigation and repair, and radical cure of chaos calls for social synergy
Jedis tool class, adapting to a single redis and redis cluster
Doris daily function summary I
Hello, people returning to work. Musk has been searching for "workaholics" again. Is telecommuting good or bad?
String 3-387. 字符串中的第一个唯一字符
Cve - 2022 - 30525 Replay of vulnerability
The 600 yuan Tiktok cloud bouncing Di live studio project is the source code of the tuyere project that rewards the income from live broadcasting
Sectigo certificate price
toggleRowSelection()失效的2个重要影响因素
Basic principle of digital circuit adder (I)
S系列·删除文件夹的几种姿势