NoClassDefFoundError和ClassNotFoundException之间的区别是什么?
是什么导致它们被抛出?如何解决这些问题?
在修改现有代码以包含新的jar文件时,我经常遇到这些可抛出的文件。
我已经在客户端和服务器端为一个通过webstart分发的java应用程序击中了它们。
我发现的可能原因是:
未包含在代码客户端build.xml中的包
我们正在使用的新jar缺少运行时类路径
版本与上一个jar冲突
当我今天遇到这些问题时,我采取了一种反复尝试的方法来让事情正常运行。我需要更多的清晰和理解。
当我需要复习的时候,我会一遍又一遍地提醒自己
ClassNotFoundException
类层次结构
ClassNotFoundException extends ReflectiveOperationException extends Exception extends Throwable
在调试
Required jar,类路径中缺少类。
验证所有必需的jar都在jvm的类路径中。
NoClassDefFoundError
类层次结构
NoClassDefFoundError extends LinkageError extends Error extends Throwable
在调试
Problem with loading a class dynamically, which was compiled properly
Problem with static blocks, constructors, init() methods of dependent class and the actual error is wrapped by multiple layers [especially when you use spring, hibernate the actual exception is wrapped and you will get NoClassDefError]
When you face "ClassNotFoundException" under a static block of dependent class
Problem with versions of class.
This happens when you have two versions v1, v2 of same class under different jar/packages, which was compiled successfully using v1 and v2 is loaded at the runtime which doesn't has the relevant methods/vars& you will see this exception. [I once resolved this issue by removing the duplicate of log4j related class under multiple jars that appeared in the classpath]
当ClassLoader没有找到所报告的类时,将抛出ClassNotFoundException。这通常意味着类从CLASSPATH中缺失。这也可能意味着这个类正试图从父类加载器中加载的另一个类中加载,因此子类加载器中的类是不可见的。在应用程序服务器等更复杂的环境中工作时,有时会出现这种情况(WebSphere因此类类加载器问题而臭名昭著)。
人们经常会混淆java.lang.NoClassDefFoundError和java.lang.ClassNotFoundException,但是它们之间有一个重要的区别。例如,一个异常(实际上是一个错误,因为java.lang.NoClassDefFoundError是java.lang.Error的子类)像
java.lang.NoClassDefFoundError:
org/apache/activemq/ActiveMQConnectionFactory
does not mean that the ActiveMQConnectionFactory class is not in the CLASSPATH. Infact its quite the opposite. It means that the class ActiveMQConnectionFactory was found by the ClassLoader however when trying to load the class, it ran into an error reading the class definition. This typically happens when the class in question has static blocks or members which use a Class that's not found by the ClassLoader. So to find the culprit, view the source of the class in question (ActiveMQConnectionFactory in this case) and look for code using static blocks or static members. If you don't have access the the source, then simply decompile it using JAD.
在检查代码时,假设你发现如下一行代码,确保类SomeClass在CLASSPATH中。
private static SomeClass foo = new SomeClass();
提示:要找出一个类属于哪个jar,您可以使用网站jarFinder。这允许您使用通配符指定类名,并在其jar数据库中搜索该类。Jarhoo允许你做同样的事情,但它不再免费使用。
如果您想在本地路径中定位类属于哪个jar,可以使用jarscan (http://www.inetfeedback.com/jarscan/)之类的实用程序。您只需指定您想要定位的类以及您希望它开始在jar和zip文件中搜索类的根目录路径。
ClassNotFoundException是一个检查异常,当我们告诉JVM使用class . forname()或ClassLoader.findSystemClass()或ClassLoader.loadClass()方法根据类的字符串名加载类,并且在类路径中没有找到所提到的类时,就会发生这种异常。
大多数情况下,当您试图运行应用程序而不使用所需的JAR文件更新类路径时,会发生此异常。例如,你可能已经看到这个异常时,做JDBC代码连接到你的数据库,即mysql,但你的类路径没有JAR。
NoClassDefFoundError发生错误当JVM试图加载一个特定的类的代码执行(作为一个正常的一部分方法调用或使用新的关键字创建一个实例的一部分),这类在类路径中不存在,但在编译时,因为为了执行程序需要编译它,如果你正在使用一个类不存在编译器会提高编译错误。
下面是简短的描述
你可以阅读关于ClassNotFoundException Vs NoClassDefFoundError的一切来了解更多细节。
当我需要复习的时候,我会一遍又一遍地提醒自己
ClassNotFoundException
类层次结构
ClassNotFoundException extends ReflectiveOperationException extends Exception extends Throwable
在调试
Required jar,类路径中缺少类。
验证所有必需的jar都在jvm的类路径中。
NoClassDefFoundError
类层次结构
NoClassDefFoundError extends LinkageError extends Error extends Throwable
在调试
Problem with loading a class dynamically, which was compiled properly
Problem with static blocks, constructors, init() methods of dependent class and the actual error is wrapped by multiple layers [especially when you use spring, hibernate the actual exception is wrapped and you will get NoClassDefError]
When you face "ClassNotFoundException" under a static block of dependent class
Problem with versions of class.
This happens when you have two versions v1, v2 of same class under different jar/packages, which was compiled successfully using v1 and v2 is loaded at the runtime which doesn't has the relevant methods/vars& you will see this exception. [I once resolved this issue by removing the duplicate of log4j related class under multiple jars that appeared in the classpath]