当我运行Java应用程序时,我得到了一个NoClassDefFoundError。造成这种情况的典型原因是什么?
当前回答
Java ClassNotFoundException vs NoClassDefFoundError
黑ClassLoader铝
静态与动态类加载
静态(隐式)类加载——引用、实例化或继承的结果。
MyClass myClass = new MyClass();
动态(显式)类加载是class . forname (), loadClass(), findSystemClass()的结果
MyClass myClass = (MyClass) Class.forName("MyClass").newInstance();
每个类都有一个ClassLoader,它使用loadClass(字符串名);这就是为什么
explicit class loader uses implicit class loader
NoClassDefFoundError是显式类装入器的一部分。Error是为了保证在编译期间出现这个类,但现在(在运行时)它不存在。
ClassNotFoundException是隐式类装入器的一部分。Exception对于可以额外使用的场景(例如反射)具有弹性。
其他回答
我从SRC库中删除了两个文件后得到了这个消息,当我把它们带回来时,我一直看到这个错误消息。
我的解决方案是:重新启动Eclipse。从那以后我就再没见过这条消息了:-)
同一项目的两个不同的签出副本
In my case, the problem was Eclipse's inability to differentiate between two different copies of the same project. I have one locked on trunk (SVN version control) and the other one working in one branch at a time. I tried out one change in the working copy as a JUnit test case, which included extracting a private inner class to be a public class on its own and while it was working, I open the other copy of the project to look around at some other part of the code that needed changes. At some point, the NoClassDefFoundError popped up complaining that the private inner class was not there; double-clicking in the stack trace brought me to the source file in the wrong project copy.
关闭项目的主干副本并再次运行测试用例可以解决这个问题。
有一个有趣的例子,你可能会看到很多NoClassDefFoundErrors:
在类的静态块中抛出一个RuntimeException 拦截它(或者如果它在测试用例中抛出并不重要) 尝试创建该类的实例
static class Example {
static {
thisThrowsRuntimeException();
}
}
static class OuterClazz {
OuterClazz() {
try {
new Example();
} catch (Throwable ignored) { //simulating catching RuntimeException from static block
// DO NOT DO THIS IN PRODUCTION CODE, THIS IS JUST AN EXAMPLE in StackOverflow
}
new Example(); //this throws NoClassDefFoundError
}
}
NoClassDefError将伴随来自静态块RuntimeException的ExceptionInInitializerError一起抛出。
当你在UNIT TESTS中看到NoClassDefFoundErrors时,这一点尤其重要。
在某种程度上,您在测试之间“共享”静态块执行,但初始的ExceptionInInitializerError将仅在一个测试用例中。第一个使用有问题的Example类。其他使用Example类的测试用例只会抛出NoClassDefFoundErrors。
这是由于您的代码所依赖的类文件在编译时存在,但在运行时没有找到。寻找构建时和运行时类路径的差异。
对此,我的解决方案是为缺失的特定类“利用”类路径内容。在我的情况下,我有2个依赖项,虽然我能够成功地编译使用javac…,我不能运行产生的类文件使用java…,因为BouncyCastle jar中的Dynamic类不能在运行时加载。
javac --classpath "ext/commons-io-2.11.0;ext/bc-fips-1.0.2.3" hello.java
因此在编译时和运行时,JVM知道在哪里获取Apache Commons和BouncyCastle依赖项,然而,当运行这个时,我得到了
Error: Unable to initialize main class hello
Caused by: java.lang.NoClassDefFoundError:
org/bouncycastle/jcajce/provider/BouncyCastleFipsProvider
因此,根据类路径,我在相同的位置手动创建了一个名为ext的新文件夹,然后在其中放置了BouncyCastle jar,以确保在运行时可以找到它。只要结果清单中指定了jar的位置,您就可以将jar相对于类文件或jar文件放置。注意,我只需要利用包含丢失类文件的一个jar。
推荐文章
- 如何在java中格式化持续时间?(如格式H:MM:SS)
- urlencoder .encode(字符串)已弃用,我应该使用什么代替?
- javax.transaction.Transactional vs . org.springframework.transaction.annotation.Transactional
- Java 8接口方法中不允许“同步”的原因是什么?
- 如何找到Java堆大小和内存使用(Linux)?
- 使用Enum实现单例(Java)
- RabbitMQ与通道和连接之间的关系
- buildSessionFactory()配置方法在Hibernate中已弃用?
- Spring MVC -如何获得所有的请求参数在一个地图在Spring控制器?
- 如何在Java中按两个字段排序?
- 文件之间的差异。路径中的分隔符和斜杠
- 在方法参数中使用NotNull注释
- Spring MVC中处理可选参数的@RequestParam
- Tomcat:如何查找正在运行的Tomcat版本?
- “java”、“javaw”和“javaws”之间有什么区别?