junit 中的 java.lang.NoClassDefFoundError
我在 junit 测试代码中的 java 中遇到此错误。我在网上查了一下,它说我需要在类路径中添加 junit.jar 。
在 Eclipse 中,我已将其添加到“项目属性”窗口的类路径中,但仍然出现初始化错误。我应该怎么办..?
这是错误的完整跟踪:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 25 more
I am getting this error in java in my junit test code. I looked up on the net and it says that I need to add the junit.jar in the classpath.
In Eclipse I have added it in the classpath of Project Properties windows but I still get initialisation error. What should I do..?
This is the complete trace of the error:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 25 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
问题是不完整的运行时类路径。您的代码可能在 IDE 中编译良好,但在运行时需要额外的类(jar)。
ClassNotFoundException: org.hamcrest.SelfDescription
指示缺少哪个类。 JUnit 需要org.hamcrest
,任何项目设置中可能缺少它。尝试执行以下步骤将所有 JUnit4 库(包括所需的运行时 jar)添加到项目的类路径中:
请注意,这些步骤适用于纯 Eclipse 项目。如果您使用 Maven、Gradle 或其他工具进行依赖项管理,则需要在其中添加依赖项并在 Eclipse 中刷新项目设置。通过正确的集成,这通常会自动发生。
The problem is an incomplete runtime classpath. Your code may compile fine in the IDE but it needs additional classes (jars) at runtime.
ClassNotFoundException: org.hamcrest.SelfDescribing
is indicating which class is missing.org.hamcrest
is required by JUnit any it may be missing in your project setup.Try the following steps to add all of the JUnit4 libraries (including required runtime jars) to the project's classpath:
Note, the steps are for a pure Eclipse project. If you use Maven, Gradle or another tool for dependency management you need to add the dependency there and refresh the project setup in Eclipse. This usually happens automatically with the proper integration.
附录:在本地我创建了一个“用户库”并添加到我的项目中
Addendum: Locally I have created a "User Library" and added to my projects which has
如果您从 JUnit 网站下载了 JUnit jar,但忘记下载 Hamcrest jar - 两者都是必需的(说明说要下载两者,但我跳过了!哎呀),也会出现同样的问题
The same problem can occur if you have downloaded JUnit jar from the JUnit website, but forgotten to download the Hamcrest jar - both are required (the instructions say to download both, but I skipped ahead! Oops)
在 Maven 中使用时,更新工件 junit:junit 从 4.8.2 到 4.11。
When using in Maven, update artifact junit:junit from e.g. 4.8.2 to 4.11.
在 Eclipse 上,我能够通过以下步骤解决上述问题:
右键单击要运行的测试文件,
选择运行方式 ->运行配置->选择类路径选项卡 ->选择引导条目 ->选择高级->选择添加库->选择JUnit->下一步 -> 从下拉列表中选择 JUnit4 ->完成
然后选择应用->运行
On Eclipse I was able to solve the above issue by following the below steps :
Right-click on the test file which you want to run,
Select Run As -> Run Configurations -> Select Classpath tab -> Select to the bootstrap Entries -> Select Advanced -> Select Add library -> Select JUnit -> Next ->Select JUnit4 from the drop-down -> Finish
Then Select Apply -> Run
就我而言,我将库添加到 Modulepath 而不是 Classpath。
仅当 JUnit 正确添加到类路径时它才有效。
In my case, I added my libraries to Modulepath instead on Classpath.
It only works if JUnit is correctly added to Classpath.
org/hamcrest/SelfDescription 类不在运行时类路径上。
The org/hamcrest/SelfDescribing class is not on the run-time classpath.
当错误显示 Filter 类丢失时,这些步骤对我有用(如这个错误重复问题中所报告的:JUnit: NoClassDefFoundError: org/junit/runner/manipulation/Filter ):
问题)。
属性,然后在运行/调试设置下删除所有条目
从该文件的启动配置。点击“应用”并关闭。
属性,然后在运行/调试设置下删除所有条目
涉及启动配置中的 JUnit。点击“应用”并关闭。
感谢这些答案为我提供了此解决方案的提示:https://stackoverflow.com/a/34067333/5538923和https://stackoverflow.com/a/39987979/5538923)。
These steps worked for me when the error showed that the Filter class was missing (as reported in this false-diplicated question: JUnit: NoClassDefFoundError: org/junit/runner/manipulation/Filter ):
problem).
Properties, and under the Run/Debug settings, remove any entries
from the Launch Configurations for that file. Hit Apply and close.
Properties, and under the Run/Debug settings, remove any entries
involving JUnit from the Launch Configurations. Hit Apply and close.
Thanks to these answers for giving me the hint for this solution: https://stackoverflow.com/a/34067333/5538923 and https://stackoverflow.com/a/39987979/5538923).
尝试以下步骤:
这对我有用。
Try below steps:
It worked for me.
我遇到了同样的问题,问题出在我的测试类中的 @ContextConfiguration 中,我也在加载 servlet 上下文,我只是将:更改
为:
就这样。
这样我只加载测试路径中具有模式 *-context.xml 的所有文件。
I had the same issue, the problem was in the @ContextConfiguration in me test classes, i was loading the servlet context too i just change:
to:
and that´s it.
this way im only loading all the files with the pattern *-context.xml in me test path.
原因是“hamcrest-core”jar 不在类路径中,因为它不直接随 junit jar 一起提供。所以有两种方法可以解决这个问题:
The reason for this is "hamcrest-core" jar is not in classpath as it doesn't comes directly with junit jar. So there are two ways to resolve this:
将我的两分钱添加到其他答案中。
检查您是否没有在
src/main/java
而不是通常的src/test/java
下创建测试类。当您出于某种原因创建新的测试类时,前者是 Eclipse 中的默认设置,可以忽略。事情就可以这么简单。Adding my two cents to other answers.
Check if you haven't by any chance created your test class under
src/main/java
instead of usualsrc/test/java
. The former is the default in Eclipse when you create a new test class for whatever reason and can be overlooked. It can be as simple as that.我正在关注此视频:https://www.youtube.com/watch?v=WHPPQGOyy_Y< /a>
但未能运行测试。之后,我删除了所有下载的文件并添加
Junit使用图中的步骤。
I was following this video: https://www.youtube.com/watch?v=WHPPQGOyy_Y
but failed to run the test. After that, I deleted all the downloaded files and add
the Junit using the step in the picture.
如果类路径中存在 hamcrest-library 或 hamcrest-core 的 2 个版本,也会出现此错误。
在 pom 文件中,您可以排除额外的版本并且它可以工作。
This error also comes if 2 versions of hamcrest-library or hamcrest-core is present in the classpath.
In the pom file, you can exclude the extra version and it works.
即使Junit4.11.jar也没有hamcrest-core.jar。我在类路径中明确添加,问题已解决。
even Junit4.11.jar doesnot have the hamcrest-core.jar. I added explicitly in the classpath and the issue was resolved.
有时,当某些模块经过测试而另一些模块没有经过测试时,会出现此错误。在这种情况下,一个可能的解决方案是将 junit 也添加到模块中,而不进行任何测试:
Sometimes this error occurs when there are some modules with and some without tests. A possible solution in this case is to add junit also to the modules without any tests:
如果你有多个版本的java,它可能会干扰你的程序。
我建议你下载JCreator。
执行此操作后,单击“配置”、“选项”和“JDK 配置文件”。从列表中删除旧版本的 Java。然后单击播放按钮。您的程序应该出现。
如果没有,请按 ctrl+alt+O,然后再次按播放按钮。
If you have more than one version of java, it may interfere with your program.
I suggest you download JCreator.
When you do, click configure, options, and JDK Profiles. Delete the old versions of Java from the list. Then click the play button. Your program should appear.
If it doesn't, press ctrl+alt+O and then press the play button again.