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.
Related
I have a folder(which is where I am running all commands) called learning. Inside this folder, I have two files, one is called Driver.java which is a simple main class with a simple hello world method. The other file is DriverTest.java which has this code as shown below.
import static org.junit.Assert.*;
import org.junit.Test;
public class DriverTest {
#Test
public void test() {
fail("Not yet implemented");
}
}
Apart from this I have JUnit 4.13-beta jar inside the same learning folder.
Now I open command line in windows and go to learning folder location and run this command.
javac -cp junit-4.13-beta-1.jar;hamcrest-core-2.1-rc4.jar;. *.java
It didn't give me any errors, hence it has compiled both Driver and DriverTest java files.
Now I am trying to run the JUnit test using this command.
java -cp junit-4.13-beta-1.jar;hamcrest-core-2.1-rc4.jar;. org.junit.runner.JUnitCore DriverTest
But I am getting this error:
JUnit version 4.13-beta-1
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
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:28)
at org.junit.runner.Request.classes(Request.java:77)
at org.junit.runner.JUnitCommandLineParseResult.createRequest(JUnitCommandLineParseResult.java:116)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
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)
... 17 more
Why is my HamCrest jar not found? Its right there in the same folder right?
You only added the current directory to the classpath for compilation. This needs to also be done for test invocation, ie. ;. in windows :. on linux
java -cp junit-4.13-beta-1.jar;. org.junit.runner.JUnitCore DriverTest
I'm getting this weird issue when my colleague runs my Jar in JRE of same version as my development environment (Java1.7 + Eclipse Indiago).
Add a Jar to build path and refer to some Classes in this jar;
Compile, run the project as Java application successfully in Eclipse;
Export the project as Runnable Jar with option - 'Package required libraries into generated Jar'.
Run the exported Jar in command line 'java -jar xxx.jar' in my computer and get expected result.
Run the same Jar in another computer with same JRE but get something error like 'ClassNotFound' telling that the class referred from inner Jar can not be found, as shown below:
Exception in thread "main" java.lang.NoClassDefFoundError: com/test/commo
n/communication/BaseNotificationEvent
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 java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:56)
Caused by: java.lang.ClassNotFoundException: com.test.common.communicatio
n.BaseNotificationEvent
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 java.lang.ClassLoader.loadClass(Unknown Source)
... 14 more
The solution so far is to export runnable jar with option 'copy required libraries into sub-folder next to generated Jar', and this works both in my and another computer.
But I am not take this as ultimate solution for I have to transfer multiple files rather a single Jar while releasing my program...which seems not elegant ^_^
I am trying to run a groovy project in windows java rembrandt.bin.Rembrandt and i got the following error
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/groovy/r
untime/BytecodeInterface8
at rembrandt.bin.Rembrandt.<init>(Rembrandt.groovy)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstru
ctor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteN
oUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstru
ctor(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor
(AbstractCallSite.java:190)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor
(AbstractCallSite.java:198)
at rembrandt.bin.Rembrandt.main(Rembrandt.groovy:383)
Caused by: java.lang.ClassNotFoundException: org.codehaus.groovy.runtime.Bytecod
eInterface8
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)
... 11 more
I am using java 8. the funny thing is that i tried to run in Linux and the program worked.In Linux i am running java 7, but already tried with java 7 in windows and nothing. I am using groovy 1.7.5 and this class( BytecodeInterface8) only appear in 1.8.0 i cant understand why this run fine in Linux and not in windows. Ty for your help
We have a jar file, which was compiled with java 1.6, and it was working fine.
Last month system were upgraded with java 8.
and now the java code gives the following error -
F:\globaltl\ConcurDump>java -jar ConcurDumpProcessLatest.jar prod
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/ApplicationContext
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:56)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.Applica
tionContext
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 java.lang.ClassLoader.loadClass(Unknown Source)
Is this issue because of the version upgrade?
The same jar used to work fine with older java version.
When i compile and run my program in eclipse, it works.
When i run my .jar program on my personal computer (Windows 8 with java JDK 7), it works.
But when i'm trying to run .jar in others computers, it doesn't work.
So i tried to run with command line java -jar myjar
and i got this message which i can't understand
java -jar "Bureau\Application SNCF\SNCF.jar"
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
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.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 java.lang.ClassLoader.loadClassInternal(Unknown Source)
Does anyone understand this message error?
Thanks for your help
This is always caused by the conflict of different Java JDK at compile time and runtime, make sure you are using same JDK version to compile and run it.