Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

junit

ant

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.

like image 795
stefina Avatar asked Jan 26 '11 20:01

stefina


3 Answers

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)
like image 111
KevinS Avatar answered Nov 16 '22 15:11

KevinS


<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>
like image 25
Edgard Leal Avatar answered Nov 16 '22 14:11

Edgard Leal


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

like image 2
CharlesTBetz Avatar answered Nov 16 '22 15:11

CharlesTBetz