gpt4 book ai didi

java - ClassNotFoundException 与 NoClassDefFoundError

转载 作者:行者123 更新时间:2023-12-02 04:15:27 25 4
gpt4 key购买 nike

我已经浏览过这个线程What causes and what are the differences between NoClassDefFoundError and ClassNotFoundException?这是线程中具有最大 ups 的 an 之一:NoClassDefFoundError :“所以,NoClassDefFoundError 出现在源代码成功编译时,但在运行时,找不到所需的类文件。这可能是可以的发生在 JAR 文件的分发或生产中,其中未包含所有必需的类文件。”

ClassNotFoundException:至于ClassNotFoundException,看起来它可能源于尝试在运行时对类进行反射调用,但程序尝试调用的类不存在。

我做了一个小实验。我创建了一个主类,类A,并尝试从中调用其他类,类B,编译成功。

然后我删除了在 A 类中调用的 B 类。我得到了 java.lang.ClassNotFoundException 但根据步骤中的答案,我应该得到 NoClassDefFoundError (源已成功编译,但在运行时未找到类文件)谁能解释一下我在解释线程中的 ans 时缺少什么?

package com.random;

public class A {

public static void main(String[] args) {
B b= new B();

}

}

package com.random;

public class B {



}

最佳答案

NoClassDefFoundError

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

<小时/>

ClassNotFoundException

Thrown when an application tries to load in a class through its string name using: The forName method in class Class. The findSystemClass method in class ClassLoader . The loadClass method in class ClassLoader.

<小时/>

你必须明白,JVM无法实现找不到你删除的class的定义,因为class > 本身无法找到,会自动抛出 ClassNotFoundException

此异常发生在运行时,因此无论它是否先编译并不重要,您删除了该文件,因此无法找到它并抛出异常 .

请注意,NoClassDefFoundError 实际上并不是异常,它是从 LinkageError 派生的 Error,而 ClassNotFoundException 派生的直接来自java.lang.Exception

要恢复,NoClassDefFoundError 在全局范围内仅意味着 JVM 尝试在运行时访问根据编译的 代码应该存在,但实际上不存在(或者不在类路径中)。

<小时/>

重现 ClassNotFoundException 的示例

public class ClassNotFoundExceptionExample {

private static final String CLASS_TO_LOAD = "main.java.Utils";

public static void main(String[] args) {
try {
Class loadedClass = Class.forName(CLASS_TO_LOAD);
System.out.println("Class " + loadedClass + " found successfully!");
}
catch (ClassNotFoundException ex) {
System.err.println("A ClassNotFoundException was caught: " + ex.getMessage());
ex.printStackTrace();
}
}
}
<小时/>

重现 NoClassDefFoundError 的示例

创建一个简单的类Test

public class Test {
public Test() {
System.out.println("A new instance of the Test class was created!");
}
}

还有一个类NoClassDefFoundErrorExample

public class NoClassDefFoundErrorExample {
private static Test test = new Test();

public static void main(String[] args) {
System.out.println("The definition of Test was found!");
}
}

现在创建一个可执行的.jar,它执行main方法。您可以在 .jar

内的 Manifest.txt 文件中指定它
Main-Class: NoClassDefFoundErrorExample

现在运行以下命令

javac Test.java
javac NoClassDefFoundErrorExample.java
jar cfm NoClassDefFoundErrorExample.jar Manifest.txt NoClassDefFoundErrorExample.class
java -jar NoClassDefFoundErrorExample.jar

注意NoClassDefFoundError

Exception in thread "main" java.lang.NoClassDefFoundError: TestClass
at NoClassDefFoundErrorExample.(NoClassDefFoundErrorExample.java:2)
Caused by: java.lang.ClassNotFoundException: TestClass
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

关于java - ClassNotFoundException 与 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56662533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com