1

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?

Community
  • 1
  • 1
ozzboy
  • 2,672
  • 8
  • 42
  • 69

2 Answers2

2

Try including hamcrest-core.jar in the classpath.

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

sps
  • 315
  • 1
  • 3
  • I still get Could not find class: com.company.division.junit.EventTests Time:0 OK (0 tests) – ozzboy Apr 21 '14 at 14:28
1

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.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Still no luck get the same error. I will try to setup a Maven. – ozzboy Apr 21 '14 at 14:54
  • @Ram what error are you getting? since this works in eclipse you have the jars locally, it's just a matter of finding where they are adding them to the java classpath. – Nathan Hughes Apr 21 '14 at 16:49
  • JUnit version 4.11 Could not find class: com.company.division.junit.EventTests Time: 0 OK (0 tests) – ozzboy Apr 21 '14 at 17:08
  • @Ram: and what command did you execute to get this? did you try the absolute-path version i added here? – Nathan Hughes Apr 21 '14 at 17:21
  • @Ram: where are your classes being written to? maybe they're being written to a different directory than the source? (another thing you can get from looking at the Configure Build Path stuff in eclipse) – Nathan Hughes Apr 24 '14 at 15:27