how to execute JUnit test in Maven? - java

I have created webdriver test for project and it works fine.
Now I have added testpackage to Maven project and did some pom.xml changes then try to execute 1 test and i have following error
Class not found com.abc.xyz.con.TestExpressMode
java.lang.ClassNotFoundException:
com.abc.xyz.con.TestExpressMode 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.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:688)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:421)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Now i did so much google and not able to fix this problem.
i have 1.8 jdk and developers have 1.6 i hope this is not a issue or?
Thanks

Related

Intellij Dependency Issue - java.lang.NoClassDefFoundError

I have started using IntelliJ for my projects recently. I was trying to develop a simple maven project and got stuck into issue. I was trying to play around with kakfa-clients. Here is my dependencies
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
I tried doing maven clean and install. I see the following error after I execute the code.
C:\KB\SDA>java -cp target/SDA-1.0-SNAPSHOT.jar org.example.code.Demo
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/kafka/clients/producer/Producer
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.kafka.clients.producer.Producer
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)
... 7 more
However I could see the kafka-clients:2.5.0 is available in the External Libraries folder
I would like to know what could have gone wrong. Any hint would definitely help.
Java version 8
IntellJ 2020.1
Kafka-Client itself is dependent on other dependencies. Have a look here https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients/2.5.0
Give a shot by including other jars in the classpath.
java -cp target/*.jar:. org.example.code.Demo

run error in eclipse when trying to run cucumber file

This Error is appears when i trying to run junit test on eclipse using cucumber
ava.lang.NoClassDefFoundError: cucumber/runtime/snippets/SummaryPrinter
at cucumber.api.junit.Cucumber.run(Cucumber.java:90)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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)
Caused by: java.lang.ClassNotFoundException: cucumber.runtime.snippets.SummaryPrinter
at java.net.URLClassLoader$1.run(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)
... 7 more
I got this error while upgrading the Cucumber version in my Maven based project.
Turn out that I updated <artifactId>cucumber-java</artifactId> version from 1.1.5 to 1.2.4, but I didn't update <artifactId>cucumber-junit</artifactId> - it was still on 1.1.5.
On the moment I updated both to the same version, this error was gone.
Class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.
Update cucumber-core,cucumber-java and cucumber-junit to the same version ( i have used 1.1.5 version ).The issue is resolved for me.

Not able to execute a Webdriver Java Code from eclipse

I recently configured Web-driver in eclipse and also configured ant and I was able to execute a project.
Recently I created 1 more project and wrote first Java code and when I click on run button , it is not able to execute it even though it has no errors.
Here is the error it is throwing when I try to execute.
Class not found Dashboard
java.lang.ClassNotFoundException: Dashboard
at java.net.URLClassLoader$1.run(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.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429)
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)
Make sure you have selenium-java-2.x.jar and JUnit/TestNG jar in you classpath. Also make sure your tests are in classpath.
To set a classpath right Click on Project -> Properties -> Java Build Path -> "Add JARs" or "Add External JARs".

Compiling Junit on windows 8 command line error

I created a Junit test case using Eclipse and it works fine. I am trying to compile it using command line but can't seem to do it.
I was able to "compile" it fine..but now when I try to run it I get the following error:
JUnit version 4.8.2
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDesc
ribing
at java.lang.ClassLoader.defineClass1(Native Method)
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$100(Unknown Source)
at java.net.URLClassLoader$1.run(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.runner.Computer.getSuite(Computer.java:26)
at org.junit.runner.Request.classes(Request.java:69)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:98)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:53)
at org.junit.runner.JUnitCore.main(JUnitCore.java:45)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader$1.run(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)
... 18 more
To run it I am using:
java -cp "E:/Android ADT/adt-bundle-windows-x86_64-20130219/eclipse/plugins/org.junit_4.8.2.v4_8_2_v20110321-1705/junit.jar;." org.junit.runner.JUnitCore SchedulerTest
The compilation worked fine, and for compiling I used:
javac -cp "E:/Android ADT/adt-bundle-windows
-x86_64-20130219/eclipse/plugins/org.junit_4.8.2.v4_8_2_v20110321-1705/junit.jar
;." SchedulerTest.java
When compilation works fine but you get java.lang.NoClassDefFoundError when running the code, that usually indicates a missing runtime dependency. As in this case, org.hamcrest.SelfDescribing is not used at compile time, but required at runtime.
hamcrest is usually bundled within the official junit.jar that you can download from junit.org, however your E:/Android ADT/adt-bundle-windows-x86_64-20130219/eclipse/plugins/org.junit_4.8.2.v4_8_2_v20110321-1705/junit.jar doesn't have it. Look for a hamcrest jar in the Android ADT plugins directory E:/Android ADT/adt-bundle-windows-x86_64-20130219/eclipse/plugins/ and include that in your classpath when you run your application.
That is, run like this:
java -cp "E:/Android ADT/adt-bundle-windows-x86_64-20130219/eclipse/plugins/PATH_TO_HAMCREST.jar;E:/Android ADT/adt-bundle-windows-x86_64-20130219/eclipse/plugins/org.junit_4.8.2.v4_8_2_v20110321-1705/junit.jar;." org.junit.runner.JUnitCore SchedulerTest
Finally, the unit test works fine in Eclipse because there hamcrest is part of the default classpath when you run unit tests.

ClassNotFoundException for TestApp when I start hazelcast/bin/run.bat

I started exploring hazelcast today I downloaded hazelcast-2.0 from the site, and following the ScreenCast provided in their site. I went to the bin directory and started run.bat from the command prompt.
I am getting the ClassNotFoundException. Can somebody please let me know what would have went wrong?
Caused by: java.lang.ClassNotFoundException: com.hazelcast.examples.TestApp
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)
As noted in duffymo's comment, there is a problem in the .bat file as it adds the following to the classpath ../lib/hazelcast-${project.version}.jar.
Just replace ${project.version} with the version number you downloaded, or check the correct jar name from the lib directory and everything should work properly.

Categories

Resources