AntBuilder can’t find package org.junit

Tags: , ,



I added some unit tests to a test directory (parallel to my src directory) in my project in Eclipse using the “JUnit test case” new file dialogue. I have two builders, the default Java Builder and an AntBuilder I added. The Java Builder continues to work, but the AntBuilder fails in Eclipse. When I select Project -> Build All, it displays this:

Buildfile: C:sourcemachine-painteclipsemachine-paintsrcbuild.xml

clean:
      [delete] Deleting directory C:sourcemachine-painteclipsemachine-paintbuild

compile:
       [mkdir] Created dir: C:sourcemachine-painteclipsemachine-paintbuildclasses
       [javac] Compiling 33 source files to C:sourcemachine-painteclipsemachine-paintbuildclasses
       [javac] C:sourcemachine-painteclipsemachine-paintteststencilBorderWalkerTest.java:3: error: package org.junit does not exist
       [javac] import static org.junit.Assert.*;
       [javac]                        ^

and then after a few similar errors, this:

 [javac] C:sourcemachine-painteclipsemachine-paintteststencilBorderWalkerTest.java:8: error: incompatible types
       [javac]  @Test
       [javac]   ^
       [javac]   required: Annotation
       [javac]   found:    Test

Here’s my build file. At this point I’m really firing in the dark, such as adding that classpath tag to the javac command.

<?xml version="1.0" encoding="UTF-8"?>
<project name="Builder" default="jar" basedir=".">
  <target name="clean">
    <delete dir="build"/>
  </target>

  <target name="compile" depends="clean">
    <mkdir dir="build/classes" />
    <javac srcdir="." destdir="build/classes" debug="true" includeantruntime="true">
      <classpath>
        <path id="org.junit" location="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib/junit.jar" />
      </classpath>
    </javac>
  </target>

  <target name="jar" depends="compile">
    <jar destfile="../../stencil.jar" basedir="build/classes">
      <manifest>
        <attribute name="Main-Class" value="stencil.Main" />
      </manifest>
    </jar>
    <copy file="../../stencil.jar" tofile="../../../robotsdoart/stencils/stencil.jar" />
  </target>

  <target name="run">
    <java jar="build/jar/stencil.jar" fork="true" />
  </target>
</project>

Answer

Try changing the srcdir to your actual source dir so it doesn’t include the tests.

Something like:

<javac srcdir="src/" destdir="build/classes" debug="true" includeantruntime="true">
  <classpath>
    <path id="org.junit" location="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib/junit.jar" /> <!-- not sure if you need this -->
  </classpath>
</javac>


Source: stackoverflow