我可以假设从 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/