java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

JavaEclipseJunitNoclassdeffounderrorHamcrest

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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=

Solution 2 - Java

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>

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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

Solution 8 - Java

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

Solution 9 - Java

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.

Solution 10 - Java

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

Solution 11 - Java

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.

Solution 12 - Java

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.

Solution 13 - Java

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.

Solution 14 - Java

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.

Solution 15 - Java

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>

Solution 16 - Java

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>

Solution 17 - Java

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.

Solution 18 - Java

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

Solution 19 - Java

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

Solution 20 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2013948View Question on Stackoverflow
Solution 1 - JavaJiacai LiuView Answer on Stackoverflow
Solution 2 - Javajkwuc89View Answer on Stackoverflow
Solution 3 - JavaMuhammed RefaatView Answer on Stackoverflow
Solution 4 - JavaGUESTView Answer on Stackoverflow
Solution 5 - JavaStefan BirknerView Answer on Stackoverflow
Solution 6 - JavaMichael OgalaView Answer on Stackoverflow
Solution 7 - JavaSaintView Answer on Stackoverflow
Solution 8 - Javaayush agrahariView Answer on Stackoverflow
Solution 9 - JavaRyan StewartView Answer on Stackoverflow
Solution 10 - JavaestudianteView Answer on Stackoverflow
Solution 11 - JavaLoganView Answer on Stackoverflow
Solution 12 - JavaRobert FView Answer on Stackoverflow
Solution 13 - JavaACVView Answer on Stackoverflow
Solution 14 - JavaDevendra SharmaView Answer on Stackoverflow
Solution 15 - JavaAlex XiaoView Answer on Stackoverflow
Solution 16 - JavaNiranjan GhuleView Answer on Stackoverflow
Solution 17 - JavaMohamed Iyeb Abed RabouView Answer on Stackoverflow
Solution 18 - JavaKarthikraja LoganathanView Answer on Stackoverflow
Solution 19 - JavaAlessandro AlvesView Answer on Stackoverflow
Solution 20 - JavaAashish SolankyView Answer on Stackoverflow