java.lang.NoClassDefFoundError:org/hamcrest/SelfDescribing

use*_*948 105 java eclipse junit noclassdeffounderror hamcrest

在运行junit测试时,eclipse我得到了这个Exception:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
Run Code Online (Sandbox Code Playgroud)

我添加了junit.jar库文件.

我试过不同版本的junit.jar的:4.4,4.8等等.

如何修复此异常?

Jia*_*Liu 88

添加hamcrest-all-X.X.jar到您的类路径.

截至2015年2月的最新版本为1.3:http://code.google.com/p/hamcrest/downloads/detail?name = hamcrest-all-1.3.jar&can = 2& q =

  • 如何将其添加到类路径或检查是否添加? (2认同)

jkw*_*c89 52

据JUnit的GitHub的团队网站(https://github.com/junit-team/junit/wiki/Download-and-Install),junit.jarhamcrest-core.jar使用JUnit 4.11时在类路径中都需要的.

这是包含junit和hamcrest的Maven依赖块.

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.1.2</version>
    <scope>test</scope>
</dependency>
<!-- Needed by junit -->
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)


Muh*_*aat 22

您必须遵循以下几个步骤:

  1. 右键单击该项目.
  2. 选择Build Path然后从其菜单中选择Add Libraries.
  3. 选择JUnit,然后单击Next.
  4. 选择JUnit4然后选择Finish.


小智 16

适用于我:IntelliJ IDEA 13.1.1,JUnit4,Java 6

我在项目路径中更改了文件:[PROJECT_NAME] .iml

取代:

  <library>
    <CLASSES>
      <root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.11.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />
  </library>
Run Code Online (Sandbox Code Playgroud)

通过:

  <library name="JUnit4">
    <CLASSES>
      <root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.11.jar!/" />
      <root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
      <root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-library-1.3.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />
  </library>
Run Code Online (Sandbox Code Playgroud)

所以最终的.iml文件是:

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="module-library">
      <library name="JUnit4">
        <CLASSES>
          <root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.11.jar!/" />
          <root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-core-1.3.jar!/" />
          <root url="jar://$APPLICATION_HOME_DIR$/lib/hamcrest-library-1.3.jar!/" />
        </CLASSES>
        <JAVADOC />
        <SOURCES />
      </library>
    </orderEntry>
  </component>
</module>
Run Code Online (Sandbox Code Playgroud)

PS:保存文件,不要让IntelliJ Idea重新加载它.就一次.


Ste*_*ner 5

您需要 junit-dep.jar,因为 junit.jar 有一个旧的 Hamcrest 类的副本。


Log*_*gan 1

这听起来像是一个类路径问题,因此有几种不同的方法可以解决它。org/hamcret/SelfDescription 从哪里来?这是你的班级还是在不同的罐子里?

尝试转到您的项目构建路径,然后在“库”选项卡上添加一个库。您应该能够为您的项目选择 JUnit。这与项目中仅包含 JUnit jar 文件有点不同。

在 JUnit 测试的运行配置中,检查类路径。您可以通过添加确保您的类路径可以看到那里的 SelfDescription 类来解决此问题。Eclipse 中的“运行”选项具有一组与 JUnit 选项不同的选项。