stefina
stefina

Reputation: 131

Ant-JUnit Error: Ant wants the JUnit .jar in it's classpath

I am a bit new to JUnit and Ant. I want to know what this error means:

The <classpath> for <junit> must include junit.jar if not in Ant's own classpath

I am compiling a Java project, and I cannot get beyond this point.

Upvotes: 13

Views: 30161

Answers (6)

Window -> Preferences -> Ant -> Runtime -> Classpath Add junit-xx.jar to the Global Entries as an external jar file.

Upvotes: -1

CharlesTBetz
CharlesTBetz

Reputation: 31

I spent a couple hours with this problem today. I had the .jar files all specified in Eclipse via Project|Properties|Java Build Path, but was still getting the

 <classpath> for <junit> must include junit.jar if not in Ant's own classpath

error when running Ant from Eclipse.

Running Ant from the command line would work fine (I had everything in the classpath environment variable).

But in Eclipse the only thing that worked was to explicitly state the classpath inside the elements, e.g.:

    <path id="JUnit 4.libraryclasspath">
        <pathelement location="...\plugins\org.junit_4.11.0.v201303080030\junit.jar"/>
        <pathelement location="...\plugins\org.hamcrest.core_1.3.0.v201303031735.jar"/>
        <pathelement location="...\lib\ant-junit4.jar"/>        
    </path>
    <path id="Ant1.classpath">
        <pathelement location="bin"/>
        <pathelement location="."/>
        <path refid="JUnit 4.libraryclasspath"/>
    </path>

... stuff...

    <target name="test1" depends="compile">
        <junit>
            <classpath refid="Ant1.classpath"/>
        </junit>

    </target>

Without explicitly specifying the classpath within the junit element, it would break in eclipse every time, even just a bare

 <junit/> 

reference

I'm no expert, just reporting what worked today.

-ctb

Upvotes: 2

Luc
Luc

Reputation: 6036

I have no clue what it means, but in my case it seems there was a conflict because of Dropbox. Restarting Netbeans resolved the issue. Might have something to do with my using Linux and classmates using Windows, but I'm not sure.

"Just restart Netbeans" might be too simple for a stackoverflow answer, but if someone had posted it, it would have saved me some time...

Upvotes: 0

Edgard Leal
Edgard Leal

Reputation: 2730

<property name="lib.dir" value="webcontent\WEB-INF\lib" />

<path id="classPath">
    <pathelement location="${lib.dir}/junit-4.11.jar" />
</path>

<target name="test" depends="build">

    <junit haltonerror="false" haltonfailure="false" fork="yes">
        <classpath path="${lib.dir}">
            <path refid="classPath" />
        </classpath>

    </junit>
</target>

Upvotes: 5

KevinS
KevinS

Reputation: 7882

The documentation of the Junit ant task gives a list of options for how to get junit.jar onto the classpath:

http://ant.apache.org/manual/Tasks/junit.html

To save you the lookup, the options are reproduced below. My preference is option 1.

  1. Put both junit.jar and ant-junit.jar in ANT_HOME/lib.
  2. Do not put either in ANT_HOME/lib, and instead include their locations in your CLASSPATH environment variable.
  3. Add both JARs to your classpath using -lib.
  4. Specify the locations of both JARs using a element in a in the build file.
  5. Leave ant-junit.jar in its default location in ANT_HOME/lib but include junit.jar in the passed to . (since Ant 1.7)

Upvotes: 13

DwB
DwB

Reputation: 38328

I believe that the following is the cause: junit.jar is not in your CLASSPATH environment variable. Either add junit.jar to your CLASSPATH or add it to the classpath that you define in your ant build file.

Here is an introduction to ant.

Upvotes: 0

Related Questions