NoClassDefFoundError: org/testng/TestNG - java

This error
NoClassDefFoundError: org/testng/TestNG
appears when I'm trying to run my test from Testng.xml file using IntelliJ IDEA.
Running my test one by one works perfectly as well as running whole gradle project which points to testng.xml, like grade core-test:test
( On my project I'm using Appium + IntelliJ + TestNG )
But when I'm running testng.xml using IntelliJ I'm immediately getting this message after pressing Run:
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 18 more

I found a solution posted here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206597869-Cannot-launch-NGTest-runner-in-IntelliJ-IDEA-2016-1
I got it to work by selecting "In whole project" (I had "In single
module" selected) under the Configuration tab in the TestNG "Run/Debug
Configurations."
However, the prior configuration worked in IntelliJ IDEA 15, so to me,
it seems that it may have come from a breaking change with newer IDE.
It worked for me.

adding a screenshot to aid Justin's answer above. The default is In single module which needs to be changed to In whole project.

For me it is a little different, i was using testng configuration file to run a testng suite. I was running it from the parent module instead of the child module. The following is what I did to overcome the error

Follow the steps -
1. Add the TestNG library from java build path - go to project --> right-click--> properties--> java build path --> third option libraries.
TestNG - sELECT and then apply and close

PLease follow following steps:
Add the TestNG library from in java build path for the project.
Right click on the project then -> Build path -> Configure build path -> Libraries tab -> Add library -> Choose TestNG and apply.
Create a testNG class and run the testng.xml file, it will work.
Right click on project -> testNG-> create testNG class -> give the details and create one method in that class then run the file.

for me, this was a little different still. I just started using IntelliJ in 2019, and it worked fine until around Christmas. Here is what finally fixed my misbehaving system that couldn't run IntelliJ (because i could run the same files from the same repository on another system running the same IntelliJ version)
Had to change the template TestNG to use a Test Kind of Suite instead of Class, then set this to whole project.
showing where i changed the project to Whole Project

Tried all the answers mentioned in this post, in addition to all this I had to remove Build from the section Before launch: Activate tool window to make it work:
Screen Shot

Mine was a little different, using Allure to generate reports with TestNG. In the Allure section of the gradle.build file, I needed to upgrade from 2.8.1 -> 2.13.8. For some reason 2.8.1 was causing the class not found exception for TestNG, some logging improvement needs to be made on their part.
def allureVersion = "2.13.8"
allure {
autoconfigure = true
aspectjweaver = true
version = allureVersion
clean = true
useTestNG {
version = allureVersion
}
}

Related

IntelliJ Jar Error : Invalid signature file digest for Manifest main attributes

I'm using IntelliJ IDE on an armx64 linux-based system, where i'm working on a non-maven java project that have a lot of modules (dependencies) linked to it. When running my project from the IDE everything works fine, however when building it into a runnable jar file and trying to run the jar from the terminal, using java -jar myjar.jar, i got the following error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:330)
at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:263)
at java.util.jar.JarVerifier.processEntry(JarVerifier.java:318)
at java.util.jar.JarVerifier.update(JarVerifier.java:230)
at java.util.jar.JarFile.initializeVerifier(JarFile.java:383)
at java.util.jar.JarFile.getInputStream(JarFile.java:450)
at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:977)
at sun.misc.Resource.cachedInputStream(Resource.java:77)
at sun.misc.Resource.getByteBuffer(Resource.java:160)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:454)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
I'm currently using OpenJdk-1.8, i tried to use Oracle JDK v1.8 instead to build and then rerun the jar file, but still facing the same error. I'd also tried to create a new simple "Hello world" application and export a corresponding jar for it where it runs successfully without errors.
Here is my jar internal structure and the corresponding manifist file:
jar structure screenshot
Thanks for #y.bedrov who linked to this issue that helped my solving the error, where it includes the following:
"sqljdbc4.jar" was the signed JAR in OP's external libraries. So,
following above approach to systematically exclude the signature
related files like .SF, .RSA or .DES or other algorithms files is the
right way to move forward.
And yes I'm also using "sqljdbc4.jar", so after reading the answer i decided to solve the problem by deleting the signature files from my Meta-inf folder inside my jar file.

class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package [duplicate]

I am using hamcrest 1.3 to test my code. It is simply a die. I am trying to test it to make sure the number generated is less than 13. I had a print statement that printed what the number generated was. The number generated was always less than 13 but the test always failed. Is there something I am doing wrong?
This is the code I am testing.
import java.util.Random;
public class Die {
private int numSides;
Random rand;
public Die(int numSides){
this.numSides = numSides;
rand = new Random(System.currentTimeMillis());
}
public int roll(){
return rand.nextInt(numSides) + 1;
}
}
And this is my test code.
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
public class DieTest {
#Test
public void testRoll() {
Die x = new Die(12);
assertThat(x.roll(), is(lessThan(13)));
}
}
Edit: This is the failure stack trace.
java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(Unknown Source)
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at DieTest.testRoll(DieTest.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
This is the site that help me solve the problem.
http://code.google.com/p/hamcrest/issues/detail?id=128
The hamcrest.jar needs to go before the Junit library in the build path.
I just removed JUnit library from my project configuration. I still can run the tests as JUnit is also included in my pom file. So the solution just use the library from Maven.
In my Eclipse inside Project settings in Java Build Path section, Libraries I have previously added internal JUnit library which uses JUnit version 4.8 and hamcrest-core version 1.1. I believe that that was causing this error in my case.
I leave this bit of information here, maybe somebody else would benefit from my experience.
If you are using a Maven project, simply remove the Junit library from the build path and instead import Junit and Hamcrest separately via POM.
Use junit-dep.jar rather than junit.jar- this is JUnit minus its dependencies. Junit.jar contains an old version of Hamcrest.
First of all make sure that you have added JUnit dependency in POM.xml file.
Now, right click on the project and go to properties, select Java build path and select Libraries tab.
In my case there were Maven dependencies, JRE and Junit4 libraries. And I just removed Junit library and it works for me. Or one can also reorder the libraries as due to build order of Hamcrest and JUnit4 the problem was occurring.
Johan Mark (above) suggested to
rename the file $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar to something like *.bak or remove the file."
Renaming/removing the file caused my Eclipse Junit library to stop working, but replacing the JAR file with a copy of the same version from my Maven repo made the certificate problem go away.
(As someone on Google remarked, the Eclipse Junit copy of hamcrest has a cert issue but the Maven copy does not...)
If you are Using Maven:
Steps:
Add Latest Hamcrest Dependency into POM, from here https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all
Add Latest JUnit Dependency into POM,from Here
https://mvnrepository.com/artifact/junit/junit
Remove Any JUnit libraries from the Build path.
As Shown here
and Here
After you have Completed all above steps , Refresh your Project and Run.
I was getting the same exception. Like beachw08 recommended, I referred to:
http://code.google.com/p/hamcrest/issues/detail?id=128
One of the posts said:
rename the file $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar to something like *.bak or remove the file.
I did this and it solved my problem.
If you get the following exception "java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package", ensure that the hamcrest jar is before the Junit library in the build path. You can configure the order in the project properties under Java Build Path on the Order and Export tab.
click below image link for more clarity :
http://i.stack.imgur.com/Y5R15.png
Removed the JUNIT 4 library from the libraries tab on Eclipse -> Java Build path and it worked.
I had the same problem.
Right Click on the project / Order and Export/ move up your hamcrest lib to the first position, for some reason it has to go first than your Junit lib
I resolved this problem by removing Junit4 library from the Build Path and added TestNG Library to the build path and imported TestNG annotations instead of Junit4 annotations in my java program.
With eclipse, I had the same issue but with mvn commandline was working. Solved it by removing Junit into the build path, not in order and export. Above example is before removing it.
I had the same problem as detailed here. I believe the problem comes down to the junit4 jar file. If, under eclipse pom editor, you look at the junit4 Hierarchy you will see that it has a dependency on hamcrest-core (i.e. hamcrest-core will, by default, be pulled in on compile). In my unit test code I use the hamcrest collection Matchers (org.hamcrest.collection). These aren't included in the core jar and I set up a dependency on hamcrest-all in the pom. Doing this duplicates the hamcrest-core inclusion and appear to leave you open to a version mismatch with the junit hamcrest-core dependency and hence the security exception. I removed the hamcrest-all dependency and replaced it with hamcrest-library and the exception went away.
If you only use core hamcrest then you should not set up your own dependency and rely on the version junit pulls in. Alternately, as suggested in another comment, use junit-dep to strip out the junit dependency and then include hamcrest-all.
i recently had this problem with eclipse and Junit.
To solve this, i did that:
1 - Download the latest hamcrest-all jar from here : https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/hamcrest/
2- Go on the eclipse installation folder: eclipse/plugin/ and find the org.hamcrest...jar
3- make a backup of the step 2 jar and replace it by the step 1 jar (rename it same as the jar step 2).
4- restart eclipse
After that, my issue was solved.
When trying to solve this problem for your particular context, keep in mind the stack trace above is merely a symptom. The solutions may work for some people, but not others.
For instance:
Putting the Hamcrest JAR before the JUnit JAR in the classpath will work in situations where the version of JUnit in use (older) contains Hamcrest classes
Overlaying the version of Hamcrest used internally by Eclipse with a 'stock' version that's been renamed to match the internal version may work if no other Eclipse plugin bundle uses manifest information in the original internal JAR
In my case, the symptom above was caused by a Hamcrest JAR used internally and provided by Eclipse, and when I tried to replaced it with a 'stock' renamed version, anything related to JUnit failed to load when I started Eclipse. After I reverted back to the original internal version, the SecurityException returned. The solution that worked for me was to delete the manifest in the JAR using 7-Zip. This effectively 'unsigned' the JAR and now my particular configuration works.
my environment Mac OS + eclipse, I found org.hamcrest.core_1.3.0.v201303031735.jar is in my JUnit 4,
so I can't make it forward than junit.jar.
so I delete it from path ~/.p2/pool/plugins/, then refresh project, it works.
This one resolved my issue :
Replace $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar with Maven or your project's lib's hamcrest-core-xx.jar (obviously renaming it to same name as eclipse jar)
I went into the build properties for the project and changed JUNIT from version 4 to version 3 and it works fine now.
Interestingly I still have version 4 in my pom.xml so I am inclined to think that this is an eclipse issue (I was able to build and run my tests via terminal just fine).
I did the following:
First in pom file i excluded hamcrest-core from junit dependency and used instead hamcrest-all. Second i removed from build path the eclipse JUNIT as it overrides the maven one. The ordering didn't affect my jars since the bad jar was excluded.
I had exactly the same issue. I created a new project and it resolved my issue.
just go to the project
then click on build path and click on configure build path
click on libraries check junit and remove it(note that junit and hamcrest in pom file)

ClassNotFoundException in IntelliJ IDEA

I have received an IntelliJ project from someone else, but it won't run even the simplest classes for me.
As a quick example, the following class will throw a ClassNotFoundException without compiler errors:
package myPackage;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
System.out.println("Soon our GUI will start from here!");
System.exit(0);
}
}
This will throw the following error:
Exception in thread "main" java.lang.ClassNotFoundException: myPackage.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:116)
Process finished with exit code 1
The file structure is as follows:
<git root>/src/myPackage/Main.java
I have set the 'src' folder as the 'source folder'. My application has no compiler errors.
I have invalidated the caches several times, but with no effect whatsoever.
In my module settings my module dependencies contain <Module source> and jdk 1.8.
I can't seem to find the answer around here (google) anywhere, and to me it seems like this error has to do with the way IntelliJ handles the project. When I first pulled the project, the src folder had NOT been set to be the source folder, which probably means I may be missing out on other settings as well.
Zack Newsham's suggestion helped me. Here is what I did:
Deleted .iml file
File > New > Project, specified the existing folder with code
completed project setup (Manifest, artifacts).
Now the project runs.
Of course there may be other reasons for ClassNotFound. However when everything seems ok or if the error comes up after it worked before, try these simple steps.

hamcrest tests always fail

I am using hamcrest 1.3 to test my code. It is simply a die. I am trying to test it to make sure the number generated is less than 13. I had a print statement that printed what the number generated was. The number generated was always less than 13 but the test always failed. Is there something I am doing wrong?
This is the code I am testing.
import java.util.Random;
public class Die {
private int numSides;
Random rand;
public Die(int numSides){
this.numSides = numSides;
rand = new Random(System.currentTimeMillis());
}
public int roll(){
return rand.nextInt(numSides) + 1;
}
}
And this is my test code.
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
public class DieTest {
#Test
public void testRoll() {
Die x = new Die(12);
assertThat(x.roll(), is(lessThan(13)));
}
}
Edit: This is the failure stack trace.
java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(Unknown Source)
at java.lang.ClassLoader.preDefineClass(Unknown Source)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at DieTest.testRoll(DieTest.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
This is the site that help me solve the problem.
http://code.google.com/p/hamcrest/issues/detail?id=128
The hamcrest.jar needs to go before the Junit library in the build path.
I just removed JUnit library from my project configuration. I still can run the tests as JUnit is also included in my pom file. So the solution just use the library from Maven.
In my Eclipse inside Project settings in Java Build Path section, Libraries I have previously added internal JUnit library which uses JUnit version 4.8 and hamcrest-core version 1.1. I believe that that was causing this error in my case.
I leave this bit of information here, maybe somebody else would benefit from my experience.
If you are using a Maven project, simply remove the Junit library from the build path and instead import Junit and Hamcrest separately via POM.
Use junit-dep.jar rather than junit.jar- this is JUnit minus its dependencies. Junit.jar contains an old version of Hamcrest.
First of all make sure that you have added JUnit dependency in POM.xml file.
Now, right click on the project and go to properties, select Java build path and select Libraries tab.
In my case there were Maven dependencies, JRE and Junit4 libraries. And I just removed Junit library and it works for me. Or one can also reorder the libraries as due to build order of Hamcrest and JUnit4 the problem was occurring.
Johan Mark (above) suggested to
rename the file $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar to something like *.bak or remove the file."
Renaming/removing the file caused my Eclipse Junit library to stop working, but replacing the JAR file with a copy of the same version from my Maven repo made the certificate problem go away.
(As someone on Google remarked, the Eclipse Junit copy of hamcrest has a cert issue but the Maven copy does not...)
If you are Using Maven:
Steps:
Add Latest Hamcrest Dependency into POM, from here https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all
Add Latest JUnit Dependency into POM,from Here
https://mvnrepository.com/artifact/junit/junit
Remove Any JUnit libraries from the Build path.
As Shown here
and Here
After you have Completed all above steps , Refresh your Project and Run.
I was getting the same exception. Like beachw08 recommended, I referred to:
http://code.google.com/p/hamcrest/issues/detail?id=128
One of the posts said:
rename the file $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar to something like *.bak or remove the file.
I did this and it solved my problem.
If you get the following exception "java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package", ensure that the hamcrest jar is before the Junit library in the build path. You can configure the order in the project properties under Java Build Path on the Order and Export tab.
click below image link for more clarity :
http://i.stack.imgur.com/Y5R15.png
Removed the JUNIT 4 library from the libraries tab on Eclipse -> Java Build path and it worked.
I had the same problem.
Right Click on the project / Order and Export/ move up your hamcrest lib to the first position, for some reason it has to go first than your Junit lib
I resolved this problem by removing Junit4 library from the Build Path and added TestNG Library to the build path and imported TestNG annotations instead of Junit4 annotations in my java program.
With eclipse, I had the same issue but with mvn commandline was working. Solved it by removing Junit into the build path, not in order and export. Above example is before removing it.
I had the same problem as detailed here. I believe the problem comes down to the junit4 jar file. If, under eclipse pom editor, you look at the junit4 Hierarchy you will see that it has a dependency on hamcrest-core (i.e. hamcrest-core will, by default, be pulled in on compile). In my unit test code I use the hamcrest collection Matchers (org.hamcrest.collection). These aren't included in the core jar and I set up a dependency on hamcrest-all in the pom. Doing this duplicates the hamcrest-core inclusion and appear to leave you open to a version mismatch with the junit hamcrest-core dependency and hence the security exception. I removed the hamcrest-all dependency and replaced it with hamcrest-library and the exception went away.
If you only use core hamcrest then you should not set up your own dependency and rely on the version junit pulls in. Alternately, as suggested in another comment, use junit-dep to strip out the junit dependency and then include hamcrest-all.
i recently had this problem with eclipse and Junit.
To solve this, i did that:
1 - Download the latest hamcrest-all jar from here : https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/hamcrest/
2- Go on the eclipse installation folder: eclipse/plugin/ and find the org.hamcrest...jar
3- make a backup of the step 2 jar and replace it by the step 1 jar (rename it same as the jar step 2).
4- restart eclipse
After that, my issue was solved.
When trying to solve this problem for your particular context, keep in mind the stack trace above is merely a symptom. The solutions may work for some people, but not others.
For instance:
Putting the Hamcrest JAR before the JUnit JAR in the classpath will work in situations where the version of JUnit in use (older) contains Hamcrest classes
Overlaying the version of Hamcrest used internally by Eclipse with a 'stock' version that's been renamed to match the internal version may work if no other Eclipse plugin bundle uses manifest information in the original internal JAR
In my case, the symptom above was caused by a Hamcrest JAR used internally and provided by Eclipse, and when I tried to replaced it with a 'stock' renamed version, anything related to JUnit failed to load when I started Eclipse. After I reverted back to the original internal version, the SecurityException returned. The solution that worked for me was to delete the manifest in the JAR using 7-Zip. This effectively 'unsigned' the JAR and now my particular configuration works.
my environment Mac OS + eclipse, I found org.hamcrest.core_1.3.0.v201303031735.jar is in my JUnit 4,
so I can't make it forward than junit.jar.
so I delete it from path ~/.p2/pool/plugins/, then refresh project, it works.
This one resolved my issue :
Replace $ECLIPSE_HOME\plugins\org.hamcrest.core_1.3.0.v201303031735.jar with Maven or your project's lib's hamcrest-core-xx.jar (obviously renaming it to same name as eclipse jar)
I went into the build properties for the project and changed JUNIT from version 4 to version 3 and it works fine now.
Interestingly I still have version 4 in my pom.xml so I am inclined to think that this is an eclipse issue (I was able to build and run my tests via terminal just fine).
I did the following:
First in pom file i excluded hamcrest-core from junit dependency and used instead hamcrest-all. Second i removed from build path the eclipse JUNIT as it overrides the maven one. The ordering didn't affect my jars since the bad jar was excluded.
I had exactly the same issue. I created a new project and it resolved my issue.
just go to the project
then click on build path and click on configure build path
click on libraries check junit and remove it(note that junit and hamcrest in pom file)

java.lang.NoClassDefFoundError in junit

I am getting this error in java in my junit test code. I looked up on the net and it says that I need to add the junit.jar in the classpath.
In Eclipse I have added it in the classpath of Project Properties windows but I still get initialisation error. What should I do..?
This is the complete trace of the error:
java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 25 more
Right click your project in Package Explorer > click Properties
go to Java Build Path > Libraries tab
click on 'Add Library' button
select JUnit
click Next.
select in dropdown button JUnit4 or other new versions.
click finish.
Then Ok.
Eclipse -> Top menu -> Run -> Run Configurations
Delete all the occurrences of your test. Your test may appear as YourTest.Method_1(). Delete that as well.
Re-run. Let Eclipse build a fresh configuration.
Addendum: Locally I have created a "User Library" and added to my projects which has
hamcrest-core-1.3.jar
junit-4.12.jar
The same problem can occur if you have downloaded JUnit jar from the JUnit website, but forgotten to download the Hamcrest jar - both are required (the instructions say to download both, but I skipped ahead! Oops)
When using in Maven, update artifact junit:junit from e.g. 4.8.2 to 4.11.
On Eclipse I was able to solve the above issue by following the below steps :
Right-click on the test file which you want to run,
Select Run As -> Run Configurations -> Select Classpath tab -> Select to the bootstrap Entries -> Select Advanced -> Select Add library -> Select JUnit -> Next ->Select JUnit4 from the drop-down -> Finish
Then Select Apply -> Run
In my case, I added my libraries to Modulepath instead on Classpath.
It only works if JUnit is correctly added to Classpath.
The org/hamcrest/SelfDescribing class is not on the run-time classpath.
These steps worked for me when the error showed that the Filter class was missing (as reported in this false-diplicated question: JUnit: NoClassDefFoundError: org/junit/runner/manipulation/Filter ):
Make sure to have JUnit 4 referenced only once in your project (I also removed the Maven nature, but I am not sure if this step has any influence in solving the
problem).
Right click the file containing unit tests, select
Properties, and under the Run/Debug settings, remove any entries
from the Launch Configurations for that file. Hit Apply and close.
Right click the project containing unit tests, select
Properties, and under the Run/Debug settings, remove any entries
involving JUnit from the Launch Configurations. Hit Apply and close.
Clean the project, and run the test.
Thanks to these answers for giving me the hint for this solution: https://stackoverflow.com/a/34067333/5538923 and https://stackoverflow.com/a/39987979/5538923).
Try below steps:
Go to your project's run configuration. In Classpath tab add JUnit library.
Retry the same steps.
It worked for me.
I had the same issue, the problem was in the #ContextConfiguration in me test classes, i was loading the servlet context too i just change:
#ContextConfiguration(locations = { "classpath*:**\*-context.xml", "classpath*:**\*-config.xml" })
to:
#ContextConfiguration(locations = { "classpath:**\*-context.xml", "classpath:**\*-config.xml" })
and that´s it.
this way im only loading all the files with the pattern *-context.xml in me test path.
Make sure Environment variable JUNIT_HOME is set to c:\JUNIT.
then In run configuration > select classpath > add external jars junit-4.11.jar
The reason for this is "hamcrest-core" jar is not in classpath as it doesn't comes directly with junit jar. So there are two ways to resolve this:
select project -> buildpath -> add libraries and select junit (It contains both junit & hamcrest-core)
download hamcrest-core from maven repo and add this to your classpath.
Adding my two cents to other answers.
Check if you haven't by any chance created your test class under src/main/java instead of usual src/test/java. The former is the default in Eclipse when you create a new test class for whatever reason and can be overlooked. It can be as simple as that.
I was following this video: https://www.youtube.com/watch?v=WHPPQGOyy_Y
but failed to run the test. After that, I deleted all the downloaded files and add
the Junit using the step in the picture.
This error also comes if 2 versions of hamcrest-library or hamcrest-core is present in the classpath.
In the pom file, you can exclude the extra version and it works.
even Junit4.11.jar doesnot have the hamcrest-core.jar. I added explicitly in the classpath and the issue was resolved.
If you have more than one version of java, it may interfere with your program.
I suggest you download JCreator.
When you do, click configure, options, and JDK Profiles. Delete the old versions of Java from the list. Then click the play button. Your program should appear.
If it doesn't, press ctrl+alt+O and then press the play button again.

Categories

Resources