0

I am trying to run JUnit tests via the command line, and I'm running into issues. Here is the code:

package tests;
import org.junit.Test;
public class InitialTwo {
    @Test
    public void test() throws Exception 
    {
        System.out.print("Testing");
    }
}

I tried following the instructions here and here but nothing seems to work. I've added the junit.jar and hamcrest.jar files to a java folder in my home directory, and also added these lines to the bash_profile:

export JUNIT_HOME="$HOME/java"
export PATH="$PATH:$JUNIT_HOME"
export CLASSPATH="$CLASSPATH:$JUNIT_HOME/junit-4.12.jar:$JUNIT_HOME/hamcrest-core-1.3.jar"

I then ran this with this result:

java -cp .:/Users/home/java/junit-4.12.jar .:/Users/home/java/hamcrest-core-1.3.jar org.junit.runner.JUnitCore InitialTwo
Error: Could not find or load main class .:.Users.home.java.hamcrest-core-1.3.jar

or this

java org.junit.runner.JUnitCore InitialTwo
Error: Could not find or load main class org.junit.runner.JUnitCore

I honestly have no idea what I'm doing wrong. The code runs and populates results when ran in Eclipse, but not on the command line.

Thanks for the help!

Community
  • 1
  • 1
kroe761
  • 3,296
  • 9
  • 52
  • 81
  • 1
    I'd recommend using Maven or Gradle. They are indispensable in the Java world. – Christopher Yang Dec 15 '15 at 16:39
  • The error suggests that Java can't find JUnitCore. I'd make sure it is where you think it is. From the command line, maybe issue: "jar -tf /Users/home/java/junit-4.12.jar | grep JUnitCore" if that doesn't turn up the class then go after that first. Also, looks like your classpath specified on the command line is incorrect--watch out for spaces and proper placement of the file delimiter (the colon for *nix). – unigeek Dec 15 '15 at 16:42
  • You might try "java -cp $CLASSPATH org.junit.runner.JUnitCore tests.InitialTwo"--just make sure you get that tests package on your classpath, also. – unigeek Dec 15 '15 at 16:51
  • unigeek, thanks! Running `jar -tf /Users/home/java/junit-4.12.jar | grep JUnitCore` shows the class. So there's that. I also ran `java -cp .:/Users/home/java/junit-4.12.jar:/Users/home/java/hamcrest-core-1.3.jar org.junit.runner.JUnitCore InitialTwo` and got `Error: Could not find or load main class org.junit.runner.JUnitCore`. Additionally, running `java -cp $CLASSPATH org.junit.runner.JUnitCore tests.InitialTwo` gives me `Error: Could not find or load main class tests.InitialTwo`. Tried with and without the `tests` both in the tests folder and in the folder preceding it. – kroe761 Dec 15 '15 at 17:16
  • So, I think you're pretty close. It should just be a matter of getting all the necessary classes on the classpath--it's a common difficulty in the beginning understanding how to line up everything. Other folks have mentioned that Maven or Gradle are helpful and it's true, but also there's value to know how these things are working which is what I think you are trying for. That last error tells you it can't find tests.InitialTwo, so this is just a classpath error.. either create a jar with tests/InitialTwo and add to $CLASSPATH or otherwise add that structure to it. – unigeek Dec 16 '15 at 15:37
  • You also seem close with this "java -cp .:/Users/home/java/junit-4.12.jar:/Users/home/java/hamcrest-core-1.3.jar org.junit.runner.JUnitCore InitialTwo". Have you tried "java -cp .:/Users/home/java/junit-4.12.jar:/Users/home/java/hamcrest-core-1.3.jar org.junit.runner.JUnitCore tests.InitialTwo"? Because you put "." on your classpath here, I believe you want to issue this command from a directory where you have relative tests/InitialTwo.class right below you. Good luck, it's just lining everything up! – unigeek Dec 16 '15 at 15:42

0 Answers0