tags:

views:

1131

answers:

2

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".

+1  A: 

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.

Liron Yahdav
That works, but it is the hackish way to do it.
DJ
+3  A: 

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.

DJ
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?
Liron Yahdav
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