ChatGPT解决这个技术问题 Extra ChatGPT

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

While running junit test in eclipse I am getting this Exception:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

I've added junit.jar library file.

I've tried different versions of junit.jar: 4.4, 4.8, etc.

How do I fix this Exception?

According to your description, junit.jar is not related to the problem
In the harcrest jar, is the hierarchy the same? as in org > hamcrest > SelfDescribing, or is it in the root folder of the jar?
this error might not be for junit jar, maybe something in your project need other jar, here there are some org.hamcrest.SelfDescribing - known versions
Thanks but i don't have anything else in the project, i've created this new project.

E
Eric Leschinski

Add hamcrest-all-X.X.jar to your classpath.

Latest version as of Feb 2015 is 1.3: http://code.google.com/p/hamcrest/downloads/detail?name=hamcrest-all-1.3.jar&can=2&q=


How can I add it to classpath or check whether it's added or not?
Agreed, how to do that would have been helpful. Luckily comment below had it.
@TomášZato first: check the file. it's hamcrest-all (it's important to get the all). In Idea-IDEs a dependency is added 1. copy/paste the file to the libs folder (or anywhere else) 2. click right the file "add as library" 3. check your import statements in your .java-files.
hamcrest-all-X.X.jar is enough while much smaller.
File --- Project Structure --- Libraries --- '+' --- Java --- ....jar (you've downloaded) --- OK, it'll show in the 'external library'.
j
jkwuc89

According to the JUnit GitHub team website (https://github.com/junit-team/junit/wiki/Download-and-Install), junit.jar and hamcrest-core.jar are both needed in the classpath when using JUnit 4.11.

Here is the Maven dependency block for including junit and hamcrest.

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

I am facing this in Junit 5 though it is supposed to work without it
D
Don Branson

A few steps you have to follow:

Right click on the project. Choose Build Path Then from its menu choose Add Libraries. Choose JUnit then click Next. Choose JUnit4 then Finish.


This fixed it for me. For someone who is not used to using java/Eclipse this was extremely helpful. I also wanted to mention that the labs provided by my Instructor for the class had a version of Junit(3 maybe) that was not working with my Lab/version of Eclipse. I had to remove it and add 4 and all is good now. Thanks a lot.
This doesn't work when you are running the build using the command prompt. This is an "IDE" only solution.
G
GUEST

Works for me: IntelliJ IDEA 13.1.1, JUnit4, Java 6

I changed the file in project path: [PROJECT_NAME].iml

Replaced:

  <library>
    <CLASSES>
      <root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.11.jar!/" />
    </CLASSES>
    <JAVADOC />
    <SOURCES />
  </library>

By:

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

So the final .iml file is:

<?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>

P.S.: save the file and don't let to IntelliJ Idea reload it. Just once.


Super awesome!! Helps me with my current project.
S
Stefan Birkner

You need junit-dep.jar because the junit.jar has a copy of old Hamcrest classes.


where is it this jar?
M
Michael Ogala

Just in case there's anyone here using netbeans and has the same problem, all you have to do is

Right click on TestLibraries

Click on Add Library

Select JUnit and click add library

Repeat the process but this time click on Hamcrest and the click add library

This should solve the problem


Thank you, this works for me. I had already the JUnit library but I was missing the Hamcrest.
S
Saint

This problem is because of your classpath miss hamcrest-core-1.3.jar. To resolve this add hamcrest-core-1.3.jar as you add junit-4.XX.jar into your classpath.

At first, I encounter this problem too, but after I refer to the official site and add hamcrest-core-1.3.jar into classpath with command line, it works properly finally.

javac -d ../../../../bin/ -cp ~/libs/junit-4.12.jar:/home/limxtop/projects/algorithms/bin  MaxHeapTest.java 

java -cp ../../../../bin/:/home/limxtop/libs/junit-4.12.jar:/home/limxtop/libs/hamcrest-core-1.3.jar org.junit.runner.JUnitCore com.limxtop.heap.MaxHeapTest

a
ayush agrahari

You need to add the hamcrest-core JAR to the classpath as described here: https://github.com/junit-team/junit4/wiki/Download-and-Install


R
Ryan Stewart

As a general rule, always make sure hamcrest is before any other testing libraries on the classpath, as many such libraries include hamcrest classes and may therefore conflict with the hamcrest version you're using. This will resolve most problems of the type you're describing.


e
estudiante

the simplest way of solving the problem to begin with is copying latest version of hamcrest-code.jar into your CLASSPATH that is the file you store other .jar files needed for compilation and running of your application.

that could be e.g.: C:/ant/lib


L
Logan

It sounds like a classpath issue, so there are a few different ways to go about it. Where does org/hamcret/SelfDescribing come from? Is that your class or in a different jar?

Try going to your project Build Path and on the Libraries tab, add a Library. You should be able to choose JUnit to your project. This is a little bit different than just having the JUnit jar file In your project.

In your Run Configuration for the JUnit test, check the Classpath. You could probably fix this by adding making sure your Classpath can see that SelfDescribing class there. The Run option in Eclipse has a different set of options for the JUnit options.


its not an issue of classpath as i've already added junit library file in the build path, even in the junit library there is a reference to hamcrest i.e. C:/devTools/eclipse-SDK-4.2.1-win32-x86_64/eclipse/plugins/org.hamcrest.core.source_1.1.0.v20090501071000.jar
thanks for your response, testNG is working fine, looks like i'll have to quit on junit
I was just thinking that JUnit actually starts a new JVM up when you run something as a unit test. That means it uses a different classpath than your project sometimes. Sometimes to debug these type of things I print the java classpath out from my code, so I can compare the differences when it executes. java-tips.org/java-se-tips/java.lang/…
R
Robert F

If this problem arise in a RCP project it can be because JUnit has been explicitly imported.

Check the editor for your plugin.xml under Dependencies tab, remove the org.junit from the Imported Packages and add org.junit to the Required Plug-ins.


A
ACV

The problem is when you set up eclipse to point to JRE instead of JDK. JRE has junit4.jar in the lib/ext folder, but not hamcrest.jar :) So the solution is to check installed JREs in Eclipse, remove the existing one and create a new one pointing to your JDK.


B
Bugs

This happens when you run Ant via command line. The implicit user dependencies are added in the classpath at the end and take precedence over the project-added classpath. Run Ant with -nouserlib flag. The implicit dependencies would be excluded from the classpath.


A
Alex Xiao

There is a better answer to solve this problem. add dependency

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-all</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

f
fcdt

The hamcrest-core-1.3.jar available on maven repository is deprecated.

Download working hamcrest-core-1.3.jar from official Junit4 github link . If you want to download from maven repository, use latest hamcrest-XX.jar.

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.2</version>
    <scope>test</scope>
</dependency>

M
MatthewSot

I had the same problem, the solution is to add in build path/plugin the jar org.hamcrest.core_1xx, you can find it in eclipse/plugins.


l
lczapski

A few steps you have to follow:

Right click on the project.

Choose Build Path & then from its menu choose Add Libraries.

Choose JUnit then click Next.

Choose JUnit4 then Finish.

This works for me...


A
Alessandro Alves

"java.lang.SecurityException: class" org.hamcrest.Matchers "'s signer information does not match signer information of other classes in the same package"

Do it: Right-click on your package click on Build Path -> Configure Build Path Click on the Libraries tab Remove JUnit Apply and close Ready.


A
Aashish Solanky

Try adding the jar files manually or try with force update with the latest hamcrest.jar


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now