120

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?

Alexei - check Codidact
  • 17,850
  • 12
  • 118
  • 126
user2013948
  • 1,201
  • 2
  • 8
  • 3
  • According to your description, `junit.jar` is not related to the problem – Andremoniy Jan 26 '13 at 16:47
  • 1
    In the `harcrest` jar, is the hierarchy the same? as in `org > hamcrest > SelfDescribing`, or is it in the root folder of the jar? – Danyel Jan 26 '13 at 16:50
  • 1
    http://stackoverflow.com/questions/1171264/ant-junit-noclassdeffounderror – Perception Jan 26 '13 at 17:00
  • yes it's in the same path junit-4.8\org\hamcrest – user2013948 Jan 27 '13 at 15:28
  • its strange tests are working in IntelliJ and In eclipse with TestNG, junit is messing with me:) – user2013948 Jan 27 '13 at 16:02
  • 1
    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](http://www.jarfinder.com/index.php/java/info/org.hamcrest.SelfDescribing) – jdurango Jan 26 '13 at 17:13
  • 1
    Thanks but i don't have anything else in the project, i've created this new project. – user2013948 Jan 27 '13 at 15:23
  • 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. – MrMesees Sep 28 '17 at 22:45

20 Answers20

100

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=

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
Jiacai Liu
  • 2,260
  • 2
  • 16
  • 32
  • 2
    How can I add it to classpath or check whether it's added or not? – Tomáš Zato - Reinstate Monica Apr 04 '15 at 14:21
  • Agreed, how to do that would have been helpful. Luckily comment below had it. – MarkII Nov 14 '15 at 22:10
  • @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. – Martin Pfeffer Apr 02 '17 at 12:06
  • `hamcrest-all-X.X.jar` is enough while much smaller. – Elist Nov 26 '17 at 12:58
  • 1
    File --- Project Structure --- Libraries --- '+' --- Java --- ....jar (you've downloaded) --- OK, it'll show in the 'external library'. – Simon Z. May 16 '19 at 06:23
60

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>
jkwuc89
  • 1,285
  • 14
  • 20
22

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.
Don Branson
  • 13,237
  • 10
  • 54
  • 98
Muhammed Refaat
  • 8,096
  • 11
  • 76
  • 108
  • 1
    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. – Tony Jul 19 '14 at 14:19
  • This doesn't work when you are running the build using the command prompt. This is an "IDE" only solution. – Partha Feb 27 '19 at 21:11
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.

GUEST
  • 171
  • 1
  • 2
5

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

Stefan Birkner
  • 21,766
  • 10
  • 52
  • 68
3

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

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
Saint
  • 1,328
  • 1
  • 6
  • 18
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

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.

Ryan Stewart
  • 115,853
  • 19
  • 167
  • 192
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

estudiante
  • 41
  • 2
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.

Logan
  • 2,299
  • 17
  • 20
  • 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 – user2013948 Jan 27 '13 at 15:31
  • thanks for your response, testNG is working fine, looks like i'll have to quit on junit – user2013948 Jan 27 '13 at 16:03
  • 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. http://www.java-tips.org/java-se-tips/java.lang/how-to-print-classpath.html – Logan Jan 27 '13 at 18:12
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.

Robert F
  • 431
  • 3
  • 11
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.

ACV
  • 8,090
  • 4
  • 56
  • 72
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.

Bugs
  • 4,356
  • 9
  • 30
  • 39
Devendra Sharma
  • 87
  • 1
  • 1
  • 12
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>
小messi
  • 13
  • 2
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>
fcdt
  • 2,151
  • 5
  • 9
  • 24
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.

MatthewSot
  • 3,166
  • 3
  • 34
  • 55
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...

lczapski
  • 3,644
  • 3
  • 10
  • 26
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.

0

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