NoClassDefFoundError 是否总是由 ClassNotFoundException 引起的?
Does NoClassDefFoundError have always ClassNotFoundException in caused by?
我可以假设从 JDK class 加载机制抛出的每个 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.
相信以上也回答了你的第二个问题。
我可以假设从 JDK class 加载机制抛出的每个 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.
相信以上也回答了你的第二个问题。