1

I have this problem that I cannot understand why it doesn't exist.

Basically, when I compile the following command:

C:\Users\Bill\My Documents\Antbook\ch04> javac -d build\test test\org\example\antbook\junit\SimpleTest.java

It gives me an error saying: "java:4 error: Package org.junit does not exist" and same with line 5. Plus, on line 9, it cannot find the "@Test".

Heres, the code:

package org.example.antbook.junit; 

//import JUnit4 classes:
import static org.junit.Assert.*;
import org.junit.Test;

public class SimpleTest
{
    @Test    
    public void testSomething() 
    {
        assertTrue("MULTIPLICATION???", 4 == (2 * 2));
    }
}

Can anyone give any suggestions? Am I meant to copy the jar files containing the Junit classes in to a directory?

Thanks.

user11998
  • 177
  • 3
  • 6
  • 13

2 Answers2

3

The best way is to put it in a dedicated folder for your libraries (ie. jar files) and include that directory in the classpath when you execute javac on the command line. In this case, if you are only using one jar file junit-4.11.jar, then this will work.

C:\Users\Bill\My Documents\Antbook\ch04> javac -cp yourLibDir\junit-4.11.jar -d build\test test\org\example\antbook\junit\SimpleTest.java

If you have more jar files, use a * to include through all the jar files in a dir on the classpath:

 javac -cp yourLibDir\*
Bizmarck
  • 2,663
  • 2
  • 33
  • 48
  • Hey bizmark, It works! Your example worked perfectly!!! Thank you so much! I blame my teacher for making these mistakes. – user11998 Apr 13 '13 at 02:52
  • You're welcome. Don't be too hard on the prof, it's the learning that counts, not the teaching :) – Bizmarck Apr 13 '13 at 02:59
2

You need to add junit-latestversion.jar under your lib folder and include it in your class path

Here is the way to add classpath from command line

java -cp jar1:jar2:jar3:dir1:. HelloWorld

Here is thje link

http://stackoverflow.com/questions/2096283/including-jars-in-classpath-on-commandline-javac-or-apt
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • Hi, I was wondering where do you put the jar file in the lib folder? I am currently using NotePad++ and I don't think I am allowed to use eclipse. – user11998 Apr 13 '13 at 02:10