junit 中的 java.lang.NoClassDefFoundError

发布于 2024-10-03 05:54:18 字数 2657 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

十年九夏 2024-10-10 05:54:18

问题是不完整的运行时类路径。您的代码可能在 IDE 中编译良好,但在运行时需要额外的类(jar)。

ClassNotFoundException: org.hamcrest.SelfDescription 指示缺少哪个类。 JUnit 需要 org.hamcrest,任何项目设置中可能缺少它。

尝试执行以下步骤将所有 JUnit4 库(包括所需的运行时 jar)添加到项目的类路径中:

  1. 在 Package Explorer > 中右键单击您的项目单击“属性”
  2. 转到“Java 构建路径”> “库”选项卡
  3. 单击“添加库”按钮
  4. ,选择 JUnit
  5. 单击“下一步”。
  6. 在下拉按钮中选择 JUnit4 或其他新版本。
  7. 单击完成。
  8. 然后好的。

请注意,这些步骤适用于纯 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:

  1. Right click your project in Package Explorer > click Properties
  2. go to Java Build Path > Libraries tab
  3. click on 'Add Library' button
  4. select JUnit
  5. click Next.
  6. select in dropdown button JUnit4 or other new versions.
  7. click finish.
  8. Then Ok.

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.

吹泡泡o 2024-10-10 05:54:18
  1. 日食->顶部菜单 ->运行->运行配置
  2. 删除所有出现的测试。您的测试可能显示为 YourTest.Method_1()。也删除它。
  3. 重新运行。让 Eclipse 构建一个新的配置。

附录:在本地我创建了一个“用户库”并添加到我的项目中

hamcrest-core-1.3.jar

junit-4.12.jar

  1. Eclipse -> Top menu -> Run -> Run Configurations
  2. Delete all the occurrences of your test. Your test may appear as YourTest.Method_1(). Delete that as well.
  3. Re-run. Let Eclipse build a fresh configuration.

Addendum: Locally I have created a "User Library" and added to my projects which has

hamcrest-core-1.3.jar

junit-4.12.jar

↘人皮目录ツ 2024-10-10 05:54:18

如果您从 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)

乖乖哒 2024-10-10 05:54:18

在 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.

榕城若虚 2024-10-10 05:54:18

在 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

魔法少女 2024-10-10 05:54:18

就我而言,我将库添加到 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.

enter image description here

断舍离 2024-10-10 05:54:18

org/hamcrest/SelfDescription 类不在运行时类路径上。

The org/hamcrest/SelfDescribing class is not on the run-time classpath.

箹锭⒈辈孓 2024-10-10 05:54:18

当错误显示 Filter 类丢失时,这些步骤对我有用(如这个错误重复问题中所报告的:JUnit: NoClassDefFoundError: org/junit/runner/manipulation/Filter ):

  1. 确保在项目中仅引用 JUnit 4 一次 (我也去掉了Maven性质,但是不确定这一步对解决问题有没有影响
    问题)
  2. 右键单击包含单元测试的文件,选择
    属性,然后在运行/调试设置下删除所有条目
    从该文件的启动配置。点击“应用”并关闭。
  3. 右键单击包含单元测试的项目,选择
    属性,然后在运行/调试设置下删除所有条目
    涉及启动配置中的 JUnit。点击“应用”并关闭。
  4. 清理项目并运行测试。

感谢这些答案为我提供了此解决方案的提示:https://stackoverflow.com/a/34067333/5538923https://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 ):

  1. Make sure to have JUnit 4 referenced only once in your project (I also removed the Maven nature, but I am not sure if this step has any influence in solving the
    problem)
    .
  2. Right click the file containing unit tests, select
    Properties, and under the Run/Debug settings, remove any entries
    from the Launch Configurations for that file. Hit Apply and close.
  3. Right click the project containing unit tests, select
    Properties, and under the Run/Debug settings, remove any entries
    involving JUnit from the Launch Configurations. Hit Apply and close.
  4. Clean the project, and run the test.

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).

不…忘初心 2024-10-10 05:54:18

尝试以下步骤:

  1. 转到项目的运行配置。在“类路径”选项卡中添加 JUnit 库。
  2. 重试相同的步骤。

这对我有用。

Try below steps:

  1. Go to your project's run configuration. In Classpath tab add JUnit library.
  2. Retry the same steps.

It worked for me.

烟酒忠诚 2024-10-10 05:54:18

我遇到了同样的问题,问题出在我的测试类中的 @ContextConfiguration 中,我也在加载 servlet 上下文,我只是将:更改

@ContextConfiguration(locations = { "classpath*:**\*-context.xml", "classpath*:**\*-config.xml" })

为:

@ContextConfiguration(locations = { "classpath:**\*-context.xml", "classpath:**\*-config.xml" })

就这样。
这样我只加载测试路径中具有模式 *-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:

@ContextConfiguration(locations = { "classpath*:**\*-context.xml", "classpath*:**\*-config.xml" })

to:

@ContextConfiguration(locations = { "classpath:**\*-context.xml", "classpath:**\*-config.xml" })

and that´s it.
this way im only loading all the files with the pattern *-context.xml in me test path.

萌吟 2024-10-10 05:54:18
  1. 确保环境变量 JUNIT_HOME 设置为 c:\JUNIT。
  2. 然后在运行配置>选择类路径>添加外部 jar junit-4.11.jar
  1. Make sure Environment variable JUNIT_HOME is set to c:\JUNIT.
  2. then In run configuration > select classpath > add external jars junit-4.11.jar
一个人练习一个人 2024-10-10 05:54:18

原因是“hamcrest-core”jar 不在类路径中,因为它不直接随 junit jar 一起提供。所以有两种方法可以解决这个问题:

  1. 选择项目->项目构建路径->添加库并选择 junit(它包含 junit 和 hamcrest-core)
  2. 从 Maven 存储库下载 hamcrest-core 并将其添加到您的类路径中。

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:

  1. select project -> buildpath -> add libraries and select junit (It contains both junit & hamcrest-core)
  2. download hamcrest-core from maven repo and add this to your classpath.
別甾虛僞 2024-10-10 05:54:18

将我的两分钱添加到其他答案中。

检查您是否没有在 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 usual src/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.

涙—继续流 2024-10-10 05:54:18

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.

enter image description here

糖粟与秋泊 2024-10-10 05:54:18

如果类路径中存在 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.

栖迟 2024-10-10 05:54:18

即使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.

浅暮の光 2024-10-10 05:54:18

有时,当某些模块经过测试而另一些模块没有经过测试时,会出现此错误。在这种情况下,一个可能的解决方案是将 junit 也添加到模块中,而不进行任何测试:

subprojects {
    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'
    }
}

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:

subprojects {
    dependencies {
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'
    }
}
趴在窗边数星星i 2024-10-10 05:54:18

如果你有多个版本的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文