problem compiling a junit test class with ant

15,521

Solution 1

Jars have to be specified by name, not by directory. Use:

<javac srcdir="src/" classpath="pathtojar/junit.jar"/>

Where "pathtojar" is the path containing the junit jar.

Solution 2

Yes, you do have to specify a CLASSPATH.

It's been a long time since I last used Eclipse, but I believe you have to right click on the project root, choose "Properties", and add the JUnit JAR to the CLASSPATH.

Ant has to know the CLASSPATH when you use it to build. Have a look at their <path> stuff.

Here's how I do it:

    <path id="production.class.path">
        <pathelement location="${production.classes}"/>
        <pathelement location="${production.resources}"/>
        <fileset dir="${production.lib}">
            <include name="**/*.jar"/>
            <exclude name="**/junit*.jar"/>
            <exclude name="**/*test*.jar"/>
        </fileset>
    </path>

    <path id="test.class.path">
        <path refid="production.class.path"/>
        <pathelement location="${test.classes}"/>
        <pathelement location="${test.resources}"/>
        <fileset dir="${test.lib}">
            <include name="**/junit*.jar"/>
            <include name="**/*test*.jar"/>
        </fileset>
    </path>

Solution 3

Actually I didn't have any luck until I used fetch.xml per apache website to pull jar dependencies. Then it worked fine for me. This is what I did:

  1. On command line I went to ant home directory.
  2. I typed “ant -f fetch.xml -Ddest=system” (stores jars in Ant's lib directory)
  3. It would not run because it wanted me to have maven-artifact-ant-2.0.4-dep.jar in ant/lib folder.
  4. I did a google search for this jar and found it at jarfinder.com.
  5. I downloaded it and put it in the Ant Lib folder as that is where it was looking for this file. It then pulled all the files it needed and I was set.
  6. Then in command line I went back to my workspace folder and reset it to build.xml ant -buildfile build.xml
  7. I then did ant clean, ant compile (build successful) and ant run and all worked - Build Successful.

JUnit is built in with Eclipse but with ANT I tried several solutions from apache and ant documentation but this is what worked for me. (I am on Windows 7) -Erik

Share:
15,521
denchr
Author by

denchr

Updated on June 04, 2022

Comments

  • denchr
    denchr almost 2 years

    I am having issues integrating junit with a working ant build.xml file. My test class is in the same directory as my source classes. As I am learning how to use ant, I simply want to compile all the source and the test classes.

    I am using eclipse and the junit test classes work fine when execute through eclipse. Which means the classpath is set up correctly (at least from eclipse's point of view) with junit.jar and ant-junit-1.7.0.jar, although I am not sure if the latter jar is absolutely necessary.

    My folder structure is:

    src/code/MyClass.java src/code/MyClassTest.java

    and the ant file contain only one target, just to compile MyClass and MyClassTest, I do not include any junit tasks at the moment, and do not mind to have the build files in the same directory as well:

    <target name="compile" >
      <javac srcdir="src/" />
    </target>
    

    Ant worked fine until I added MyClassTest.java (Junit with annotations) in my folder. The output is:

    [javac] C:\....\src\MyClassTest.java:3: package org.junit does not exist
    cannot find symbol
    

    My thinking is that somehow Ant can't find the junit libraries. Since I do not specify a classpath, I was assuming that Ant would look at the same location as the source files to find to find what it needs...How can I tell Ant that the junit jars are right there?

    Any ideas, are really appreciated. Regards

  • denchr
    denchr almost 15 years
    Thanks Marco and Duffy. Even though according to the eclipse package explorer view the junit.jar is referenced right under the root project name, I had to point to the jar directly via its absolute location.. I mean classpath="/junit.jar" just didn't work, I had to say classpath="C:\Users\Desktop\Work\Eclipse\plugins\org.junit4_‌​4.3.1\junit.jar" Thanks again!