NoClassDefFoundError 和 ClassNotFoundException 之间的原因和区别是什么?
NoClassDefFoundError
和ClassNotFoundException
有什么区别?
是什么导致它们被抛出?如何解决?
What causes them to be thrown? How can they be resolved?
在修改现有代码以包含新的 jar 文件时,我经常会遇到这些 throwable.对于通过 webstart 分发的 java 应用程序,我在客户端和服务器端都点击了它们.
I often encounter these throwables when modifying existing code to include new jar files. I have hit them on both the client side and the server side for a java app distributed through webstart.
我遇到的可能原因:
- 客户端代码的
build.xml
中未包含的包 - 我们正在使用的新 jar 缺少运行时类路径
- 版本与之前的 jar 冲突
当我今天遇到这些问题时,我会采取循序渐进的方法来让事情顺利进行.我需要更多的清晰和理解.
When I encounter these today I take a trail-and-error approach to get things working. I need more clarity and understanding.
推荐答案
与Java API Specifications的区别如下.
The difference from the Java API Specifications is as follows.
对于 ClassNotFoundException
:
当应用程序尝试通过它的字符串加载一个类名称使用:
Thrown when an application tries to load in a class through its string name using:
Class
类中的forName
方法.ClassLoader
类中的findSystemClass
方法.ClassLoader
类中的loadClass
方法.
- The
forName
method in classClass
. - The
findSystemClass
method in classClassLoader
. - The
loadClass
method in classClassLoader
.
但没有定义类可以找到指定的名称.
but no definition for the class with the specified name could be found.
对于 NoClassDefFoundError
:
如果 Java 虚拟机或ClassLoader
实例尝试加载在类的定义中(作为一部分正常的方法调用或作为使用 new 创建一个新实例表达式)并且没有定义可以找到类.
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.
所以,似乎NoClassDefFoundError
发生在成功编译源时,但在运行时,找不到所需的class
文件.这可能是在 JAR 文件的分发或生产中可能发生的事情,其中并未包含所有必需的 class
文件.
So, it appears that the NoClassDefFoundError
occurs when the source was successfully compiled, but at runtime, the required class
files were not found. This may be something that can happen in the distribution or production of JAR files, where not all the required class
files were included.
至于ClassNotFoundException
,看来它可能源于试图在运行时对类进行反射调用,但程序试图调用的类并不存在.
As for ClassNotFoundException
, it appears that it may stem from trying to make reflective calls to classes at runtime, but the classes the program is trying to call is does not exist.
两者的区别在于,一个是Error
,一个是Exception
.NoClassDefFoundError
是一个 Error
,它是由 Java 虚拟机在查找它期望找到的类时遇到问题而引起的.由于未找到 class
文件,或者与在编译时生成或遇到的文件不同,因此预期在编译时工作的程序无法运行.这是一个非常严重的错误,因为程序无法由 JVM 启动.
The difference between the two is that one is an Error
and the other is an Exception
. With NoClassDefFoundError
is an Error
and it arises from the Java Virtual Machine having problems finding a class it expected to find. A program that was expected to work at compile-time can't run because of class
files not being found, or is not the same as was produced or encountered at compile-time. This is a pretty critical error, as the program cannot be initiated by the JVM.
另一方面,ClassNotFoundException
是一个Exception
,因此在某种程度上是可以预料的,并且是可以恢复的.使用反射可能容易出错(因为有些期望事情可能不会按预期进行.没有编译时检查以查看所有必需的类是否存在,因此查找所需类的任何问题都会在运行时出现.
On the other hand, the ClassNotFoundException
is an Exception
, so it is somewhat expected, and is something that is recoverable. Using reflection is can be error-prone (as there is some expectations that things may not go as expected. There is no compile-time check to see that all the required classes exist, so any problems with finding the desired classes will appear at runtime.
相关文章