Ant -JUnit Error: Ant wants JUnit.jar in it the class path

Hi guys, I'm a little 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 own classpath 

I am compiling a Java project and I cannot go beyond that.

+8
java junit ant
source share
5 answers

The documentation for the Junit ant task provides a list of options for getting junit.jar in the classpath:

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

To save your search, the parameters are reproduced below. My preference is option 1.

  • Put both junit.jar and ant -junit.jar in ANT_HOME / lib.
  • Do not put in ANT_HOME / lib, but instead indicate their location in your CLASSPATH environment variable.
  • Add both JARs to your classpath with -lib.
  • Specify the location of both JARs using the item in the assembly file.
  • Leave ant -junit.jar in your default location in ANT_HOME / lib, but enable junit.jar in the passed one. (since ant 1.7)
+7
source share
 <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> 
+4
source share

I believe the reason is this: junit.jar is not part of the CLASSPATH environment variable. Either add junit.jar to your CLASSPATH, or add it to the class path that you define in your ant build file.

Here is an introduction to ant .

0
source share

I have no idea what this means, but in my case it seems that the conflict was due to Dropbox. Restarting Netbeans resolved the issue. It may have something to do with my use of Linux and classmates using Windows, but I'm not sure.

"Just restarting Netbeans" might be too simple for a stackoverflow answer, but if someone posted it, it would save me some time ...

0
source share

Today I spent a couple of hours with this problem. I had .jar files all specified in Eclipse via Project | Properties | Java build path but still getting

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

when running Ant from Eclipse.

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

But in Eclipse, the only thing that worked was to explicitly specify the class path inside the elements, for example:

  <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 class path in the junit element, it will break the eclipse every time, even just bare

  <junit/> 

link

I am not an expert, I just report that it works today.

-ctb

0
source share

All Articles