0

I have junit4.jar in a folder along with TestMain.java

I successfully compiled test main, then I attempt to run it from command line with

java -cp junit4.jar org.junit.runner.JUnitCore TestMain

I get this error message

JUnit version 4.10
Could not find class: TestMain

But they're in the same folder! What am I missing here?

ThisClark
  • 14,352
  • 10
  • 69
  • 100
  • it might belong into the `libs` directory and the class into the `test` directory. – Martin Zeitler Sep 06 '18 at 01:07
  • I don't have a project with directories. I have TestMain and junit.jar in one folder together. I need to run junit on TestMain which in turn runs a class loader and loads some other classes in that folder. – ThisClark Sep 06 '18 at 04:30

1 Answers1

0

I think you need to add the current directory to the classpath.

Try (I added :. after junit-4.10.jar)

java -cp junit-4.10.jar:. org.junit.runner.JUnitCore TestMain

Please note that the JUnit version in your error message is different from the -cp jar version. I used version 4.10 because this is the one your error message shows.

Source: this Stack Overflow answer

Daniel Muñoz
  • 1,374
  • 12
  • 18
  • I tried that and it did not work. I get new error, `Error: Could not find or load main class org.junit.runner.JUnitCore` In my question, I was loading junit just fine. The test runner just can't find the TestMain class also in the same folder. – ThisClark Sep 04 '18 at 03:11
  • @ThisClark what operating system do you use? https://stackoverflow.com/questions/4528438/classpath-does-not-work-under-linux – piradian Sep 04 '18 at 05:09
  • Windows Linux or OSX – ThisClark Sep 04 '18 at 11:26
  • @ThisClark your original error message says your JUnit version is 4.10. Therefore your jar should be junit-4.10.jar. I updated the answer. – Daniel Muñoz Sep 06 '18 at 00:21
  • I renamed the jar. That’s irrelevant, but for what it's worth I put the name back to junit-4.10.jar and still no difference. In fact the `:.` at the end makes it completely not work. The problem is finding TestMain not running Junit. To be clear, JUnit is working okay, but it just doesn't see the class in the same folder with that name, TestMain. – ThisClark Sep 06 '18 at 04:24