20

When I use a Java IDE to build projects (e.g. NetBeans) that have JUnit tests, they compile fine, but when I try to use ant outside of the IDE to run the build script, I get the error "package org.junit does not exist".

3 Answers 3

12

You should add your junit.jar into the classpath definition in your ant file.

There are many way to do it, one example is:

<junit printsummary="yes" haltonfailure="yes">
    <classpath>
        <path refid="your.classpath.refid" />
        <fileset dir="${junit.dir}">
            <include name="**/junit.jar" />
        </fileset>
    </classpath>
    ...
</junit>

See Ant Manual for details on setting up your classpath.

2
  • How come NetBeans uses ant to run JUnit tests fine, but when I run the same ant build script outside the IDE it wouldn't find JUnit? Dec 1, 2009 at 18:47
  • 5
    Most IDEs, like NetBeans and Eclipse, include junit.jar in the classpath automatically as part of their feature supporting unit testing within the IDE. Even when you use the IDE to run your ant build, the IDE has the classpath environment setup already so ant is able to find your junit.jar.
    – DJ.
    Dec 1, 2009 at 22:19
3

The problem was that in the IDE, it set the classpath correctly to include the .jar for JUnit. Running ant outside the IDE, the classpath was different, thus the error. The fix was to put the JUnit .jar in the folder "C:\Program Files\Java\jre6\lib\ext" so it would always be found outside of any IDE.

0
3

Late answer here.

Copy the junit.jar file to the ${ANT_HOME}/lib folder.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.