ozzboy
ozzboy

Reputation: 2720

Error when running JUnit Tests from DOS Command line

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?

Upvotes: 1

Views: 1783

Answers (2)

Nathan Hughes
Nathan Hughes

Reputation: 96454

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.

Upvotes: 1

sps
sps

Reputation: 325

Try including hamcrest-core.jar in the classpath.

https://github.com/junit-team/junit/wiki/Download-and-Install

Upvotes: 2

Related Questions