gpt4 book ai didi

java - NoClassDefFoundError 是否总是由 ClassNotFoundException 引起?

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

我可以假设从 JDK 类加载机制抛出的每个 NoClassDefFoundError 总是在堆栈跟踪中将 ClassNotFoundException 作为原因吗?

另外,NoClassDefFoundError实际上是在哪里抛出的,并且它的原因被初始化为ClassNotFoundException?我找不到负责该逻辑的 Java 代码。

这就是我通常看到的堆栈跟踪:

Exception in thread "main" java.lang.NoClassDefFoundError: package/Missing
at package.Missing(Missing.java:110)
Caused by: java.lang.ClassNotFoundException: package.Missing
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

最佳答案

Can I assume that every NoClassDefFoundError thrown from JDK class loading mechanism will always have ClassNotFoundException as a cause in stacktrace?

这是一个合理的假设。根据Java Virtual Machine Specification :

If the Java Virtual Machine ever attempts to load a class C during verification (§5.4.1) or resolution (§5.4.3) (but not initialization (§5.5)), and the class loader that is used to initiate loading of C throws an instance of ClassNotFoundException, then the Java Virtual Machine must throw an instance of NoClassDefFoundError whose cause is the instance of ClassNotFoundException.

我相信以上也回答了你的第二个问题。

关于java - NoClassDefFoundError 是否总是由 ClassNotFoundException 引起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50001199/

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