java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
Asked Answered
H

21

131

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?

Herbert answered 26/1, 2013 at 16:41 Comment(8)
According to your description, junit.jar is not related to the problemGrenville
In the harcrest jar, is the hierarchy the same? as in org > hamcrest > SelfDescribing, or is it in the root folder of the jar?Unfold
#1171764Cernuous
this error might not be for junit jar, maybe something in your project need other jar, here there are some org.hamcrest.SelfDescribing - known versionsNobukonoby
Thanks but i don't have anything else in the project, i've created this new project.Herbert
yes it's in the same path junit-4.8\org\hamcrestHerbert
its strange tests are working in IntelliJ and In eclipse with TestNG, junit is messing with me:)Herbert
had this exact same thing in netbeans, first ensure tests are in test packages (likely IDE weirdness). Then ensure both hamcrest and junit are in test libraries.Deft
E
115

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=

Enlace answered 28/9, 2013 at 8:14 Comment(5)
How can I add it to classpath or check whether it's added or not?Birdiebirdlike
Agreed, how to do that would have been helpful. Luckily comment below had it.Syllabus
@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.Lunatic
hamcrest-all-X.X.jar is enough while much smaller.Rintoul
File --- Project Structure --- Libraries --- '+' --- Java --- ....jar (you've downloaded) --- OK, it'll show in the 'external library'.Nichellenichol
M
63

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>
Mealy answered 20/12, 2013 at 13:47 Comment(1)
I am facing this in Junit 5 though it is supposed to work without itProgesterone
P
23

A few steps you have to follow:

  1. Right click on the project.
  2. Choose Build Path Then from its menu choose Add Libraries.
  3. Choose JUnit then click Next.
  4. Choose JUnit4 then Finish.
Pergola answered 23/6, 2013 at 10:36 Comment(2)
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.Platino
This doesn't work when you are running the build using the command prompt. This is an "IDE" only solution.Gambrinus
S
17

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.

Secret answered 9/4, 2014 at 22:29 Comment(1)
Super awesome!! Helps me with my current project.Syllabus
O
5

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

Outdate answered 27/1, 2013 at 17:19 Comment(1)
where is it this jar?Napkin
M
4

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

Myocardium answered 28/10, 2016 at 11:8 Comment(1)
Thank you, this works for me. I had already the JUnit library but I was missing the Hamcrest.Bougainville
D
3

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
Disject answered 8/6, 2017 at 2:18 Comment(0)
C
3

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

Cadaverous answered 28/7, 2018 at 16:44 Comment(0)
E
2

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.

Egger answered 27/1, 2013 at 17:42 Comment(0)
G
2

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

Gigot answered 1/2, 2017 at 9:43 Comment(0)
E
1

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.

Epact answered 26/1, 2013 at 16:59 Comment(3)
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.jarHerbert
thanks for your response, testNG is working fine, looks like i'll have to quit on junitHerbert
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/…Epact
P
1

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.

Pazit answered 12/9, 2013 at 14:19 Comment(0)
P
1

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.

Papst answered 10/3, 2016 at 9:19 Comment(0)
T
1

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.

Terrel answered 22/2, 2018 at 0:44 Comment(0)
O
1

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>
Oilstone answered 21/5, 2019 at 6:5 Comment(0)
E
1

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>
Endocentric answered 18/10, 2020 at 20:36 Comment(0)
M
0

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.

Making answered 17/9, 2013 at 22:1 Comment(0)
T
0

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

Teagan answered 28/10, 2019 at 12:59 Comment(0)
R
0

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

Runion answered 18/2, 2021 at 18:47 Comment(0)
F
0

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

Fiume answered 3/3, 2021 at 6:24 Comment(0)
P
0

After spending 2hr, I found following jar files are compatible with RestAssure API calls.

commons-lang3-3.11.jar commons-logging-1.2.jar groovy-4.0.11.jar groovy-json-4.0.11.jar groovy-xml-4.0.11.jar hamcrest-all-1.3.jar httpclient-4.5.13.jar httpcore-4.4.4.jar httpcore-nio-4.4.12.jar httpmime-4.3.6.jar rest-assured-5.3.2.jar rest-assured-all-5.3.2.jar tagsoup-1.2.1.jar testng-7.8.0.jar unirest-java-1.4.9.jar

Prana answered 29/11, 2023 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.