My projects is running on a Windows 2008 Server.
My project source code sit in the following folders:
com.company.division.dao
com.company.division.entity
com.company.division.main
com.company.division.junit
My junit test class are in the folder C:\Sample\com\company\division\junit.
I am using JUnit 4.11
When I run this from Eclipse it runs fine.
I have tried the following from DOS command line:
First I cd C:\Sample\com\company\division\junit\
From C:\Sample\com\company\division\junit> where the compiled EventTests.class file exists I run:
java -classpath "C:\Sample\lib\junit.jar;" org.junit.runner.JUnitCore com.company.division.junit.EventTests
OR
java -cp "C:\Sample\lib\junit.jar;" org.junit.runner.JUnitCore com.company.division.junit.EventTests
OR
java -cp ".;C:\Sample\lib\junit.jar;" org.junit.runner.JUnitCore com.company.division.junit.EventTests
But nothing seems to work. I get an exception:
JUnit version 4.11
Could not find class: com.company.division.junit.EventTests
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:75)
at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:96)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
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
Edited - New update
So from one of the other threads I figured out what the command eclipse was using to run the unit tests. Eclipse was using javaw.exe, I replaced javaw with java.exe tool.
Here is the command,
C:\Java\64bit_Browser_Plugin\bin\java.exe -Dfile.encoding=Cp1252 -classpath C:\working_dir\bin;C:\working_dir\lib\mysql-connector-java-5.0.8-bin.jar;C:\working_dir\lib\ojdbc14.jar;C:\working_dir\lib\spring-beans-3.2.4.RELEASE.jar;C:\working_dir\lib\spring-context-3.2.4.RELEASE.jar;C:\working_dir\lib\spring-core-3.2.4.RELEASE.jar;C:\working_dir\lib\spring-expression-3.2.4.RELEASE.jar;C:\working_dir\lib\spring-jdbc-3.2.4.RELEASE.jar;C:\working_dir\lib\spring-tx-3.2.4.RELEASE.jar;C:\working_dir\lib\commons-logging-1.1.3.jar;C:\working_dir\lib\nzjdbc.jar;C:\working_dir\lib\junit.jar;C:\working_dir\lib\spring-batch-core-2.2.5.RELEASE.jar;C:\working_dir\lib\spring-batch-infrastructure-2.2.5.RELEASE.jar;C:\working_dir\lib\hamcrest-core-1.3.jar;C:\working_dir\lib\log4j-1.2.14.jar;C:\working_dir\lib\mail.jar;C:\working_dir\lib\spring-context-support-4.0.1.RELEASE.jar;/C:/eclipse/configuration/org.eclipse.osgi/bundles/167/1/.cp/;/C:/eclipse/configuration/org.eclipse.osgi/bundles/166/1/.cp/ org.eclipse.jdt.internal.junit.runner.RemoteTestRunner -version 3 -port 54536 -testLoaderClass org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader -loaderpluginname org.eclipse.jdt.junit4.runtime -classNames com.omnitracs.fra.junit.EventTests
I am still getting an exception:
Could not connect to: : 54536
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.connect(Remote
TestRunner.java:570)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTest
Runner.java:381)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTes
tRunner.java:197)
What am I missing?
Try including hamcrest-core.jar in the classpath.
https://github.com/junit-team/junit/wiki/Download-and-Install
Changing directories to where your test is located is a mistake. The java program will start from the classpath you give it and assume the package directory structure starts from there, so if you are in C:\Sample\com\company\division\junit\ and you give the java command -cp = "." then it will look for the class com.company.division.junit.EventTests in C:\Sample\com\company\division\junit\com\company\division\junit\.
Also if you don't include your classes in the classpath (your last example is the only one to include them) then the java exe won't find them.
You need to add the current directory to the classpath, first changing directories so that the top of the package structure is at your current directory:
cd c:\Sample
java -cp ".;lib\*.jar" org.junit.runner.JUnitCore com.company.division.junit.EventTests
This way . points to where your packages are defined.
If you would rather use absolute paths then try
java -cp "C:\Sample;C:\Sample\lib\*.jar" org.junit.runner.JUnitCore com.company.division.junit.EventTests
You can use the * as a wildcard to include multiple jars starting with jdk 6.
For the future, consider using the directory layout used by default in Maven. Keeping tests separate from code is typically a good thing. I wonder if part of this problem isn't that test-only dependencies are located in a different spot than other libraries.
Right-click on your project in Eclipse and find the menu Build Path -> Configure Build Path, and make sure you know where Eclipse is getting these jars from.
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 have a java program that uses dynamic reports fine in netbeans, and even works in exe and jar when I first run it. However, if I share the jar/exe (I've even sent it and re-downloaded it on my own computer), I get an error java.lang.classnotfoundexception: net.sf.jasperreports.engine.jrdatasource. However, I have double checked my jars and I do have this class. What could be the source of this problem then?
Thanks
Edit: full error:
Exception in thread "main" java.lang.NoClassDefFoundError:
net/sf/jasperreports/engine/JRDataSource
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:
net.sf.jasperreports.engine.JRDataSource
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
if you get Caused by: java.lang.ClassNotFoundException: inside a jar file then, you need to provide the classpath arguments;
example:
java -cp my.jar -jar ExternalJarFileName.jar
you can also provide all jars use below command, only need to create a directory and put all external jars in to it and use path of directory in to below command:
java -cp my.jar;C:\externalJar\* mainpackage.MainClass
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.
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.
after export from eclipse i have error:
C:\Program Files\Java\jre6\bin>java C:\wamp\www\JOGL\test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: C:\wamp\www\JOGL\test/jar
Caused by: java.lang.ClassNotFoundException: C:\wamp\www\JOGL\test.jar
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)
Could not find the main class: C:\wamp\www\JOGL\test.jar. Program will exit.
manifest from this file:
Manifest-Version: 1.0
Sealed: true
Main-Class: windows.SimpleScene
the same error when export with Fat Jar
updated:
C:\Program Files\Java\jre6\bin>java -jar C:\wamp\www\JOGL\test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/opengl/GLEventListener
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)
Caused by: java.lang.ClassNotFoundException: javax.media.opengl.GLEventListener
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)
... 12 more
Could not find the main class: windows.SimpleScene. Program will exit.
To run a jar file, you run:
java -jar (name-of-jar-file)
So you should run:
java -jar C:\wamp\www\JOGL\test.jar
Make sure, when you are exporting and creating you JAR three things are set:
Export it as a "runnable jar file".
Define your launch configuration (which is your class that has the main method in it).
select "Package Required libraries into generated JAR".
The command you entered only "java file.jar" is used to run .class files, the right syntax to run jar is the "java -jar file.jar" that search your jar for the manifest and execute it.
You can also run it by adding in the class-path and the main class name
java -cp C:\wamp\www\JOGL\test.jar com.ext.Example
That's not how you run a JAR file!
Use java -jar test.jar syntax
The reason you are seeing this error is java is looking at your current working directory as the classpath. where as your JAR file is located in a different directory.
You have the following options:
1)
cd C:\wamp\www\JOGL
java -jar test.jar (as you defined the Main-Class in the manifest file, java would pick it from there).
Make sure your jar maintains the same directory structure as your package structure.
2) java -cp C:\wamp\www\JOGL\test.jar window.SimpleClass (pointing the classpath to your jar)
Note: If you have dependencies on other JARs/classes beyond this JAR file, they need to be part of the classpath (absolute/relative to current directory).