Sunday, October 30, 2011

JUnit exception: java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

Problem

Although having the junit.jar on a JEE project, when running the junit test class, the following exception is generated:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

Solution


Goto http://www.jarfinder.com/ 


On the class search tab, search for the missing class shown on the stack trace output, typing:


org.hamcrest



On the output list it's shown newer versions.
Choose one, for instance junit-4.4.jar and download it using the  jar search tab beside the class search tab.

Switch the older version of the jar file on the project by the newer one.
To check, refresh your project, rebuild if necessary.
On the junit class file, type part of the import statement to activate the autocomplete. For instance:

import org.hamc

At this point, if using netbeans or eclipse, the autocomplete will show the options.
If the options shown are respective to the missing class, the problem is solved and you're ready to run the junit class.

The same procedure can be used for such other problems.



eclipse: java: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" or Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

  >PROBLEM Using Eclipse, you try to run a simple logging test using "org.slf4j.Logger" like the sample below: package Test; im...