0

Back 3 months ago I asked a similar question: Error when running JUnit Tests from DOS Command line

I haven't solved the problem yet so I simplified the process by trying to run a simple sample TestCase Class from my Windows DOS command line.

My TestCase Class is:

package Test;
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
public class TestCaseA 
{
      @Before
       public void beforeMethod()
       {
           System.out.println("Before method..");
       }

       @Test
       public void JUnit4Test()
       {
          System.out.println("In test method");
       }

       @After
       public void afterMethod()
       {
          System.out.println("after method");
       }

}

I have a folder E:\Jenkins\Test\ where my compiled .class file exists and jUnit jar insdie E:\Jenkins\Test\lib\

See attached screen shots:

enter image description here

enter image description here

The command I am running to execute this from command line on a Windows Server MS DOS prompt is:

1. I Change Directory into E:\Jenkins\Test
2. E:\Jenkins\Test>java -cp E:\Jenkins\Test\junit-4.10.jar;Test\TestCaseA.class; org.junit.runner.JUnitCore Test.TestCaseA

The output:

JUnit version 4.10
Could not find class: Test.TestCaseA

Time: 0

OK (0 tests)

enter image description here

What am I missing? Please help.

Community
  • 1
  • 1
ozzboy
  • 2,672
  • 8
  • 42
  • 69
  • Does it work if CP has `...;*E:\Jenkins\*Test\TestCaseA.class`? (Asterisks to highlight) – AlexR Jul 24 '14 at 18:01
  • E:\Jenkins\Test>java -cp E:\Jenkins\Test\lib\junit-4.10.jar;E:\Jenkins\Test\TestCaseA.class; org.junit.runner.JUnitCore Test.TestCaseA I think this is what you meant. No this throws the same error @AlexR – ozzboy Jul 24 '14 at 18:27
  • Okay last guess. What happens if you jar the .class files and add the .jar to the cp? – AlexR Jul 24 '14 at 18:43
  • This looks like a classpath issue. For a package _Test_ with a class file _X:\foo\Test\TestCaseA.class_ the directory you should include in the classpath is `X:\foo` – McDowell Jul 24 '14 at 19:07
  • @McDowell - Yes that worked! Can you add that as an answer so I can give you credit. – ozzboy Jul 24 '14 at 19:31
  • E:\Jenkins\Test>java -cp E:\Jenkins\Test\lib\junit-4.10.jar;E:\Jenkins\;E:\Jenkins\Test\TestCaseA.class; org.junit.runner.JUnitCore Test.TestCaseA – ozzboy Jul 24 '14 at 19:32

1 Answers1

0

This looks like a classpath issue. For a package Test with a class file X:\foo\Test\TestCaseA.class the directory you should include in the classpath is X:\foo

>java -cp E:\Jenkins\Test\lib\junit-4.10.jar;E:\Jenkins\ org.junit.runner.JUnitCore Test.TestCaseA
McDowell
  • 107,573
  • 31
  • 204
  • 267
  • Please read this link on how to include multiple jars https://javarevisited.blogspot.com/2012/10/5-ways-to-add-multiple-jar-to-classpath-java.html – MSS Jul 09 '19 at 12:14