Dave Aaron Smith
Dave Aaron Smith

Reputation: 4567

AntBuilder can't find package org.junit

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:\source\machine-paint\eclipse\machine-paint\src\build.xml

clean:
      [delete] Deleting directory C:\source\machine-paint\eclipse\machine-paint\build

compile:
       [mkdir] Created dir: C:\source\machine-paint\eclipse\machine-paint\build\classes
       [javac] Compiling 33 source files to C:\source\machine-paint\eclipse\machine-paint\build\classes
       [javac] C:\source\machine-paint\eclipse\machine-paint\test\stencil\BorderWalkerTest.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:\source\machine-paint\eclipse\machine-paint\test\stencil\BorderWalkerTest.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>

Upvotes: 0

Views: 6851

Answers (3)

Creating a path element and referring that id in javac helped.

<path id="lib.path.id.junit.jar">
    <fileset dir="${project-path}\lib" includes="*.jar" />
</path>

And then I referred it in javac using classpathref

<target name="compile" depends="copy" description="compile all the source files in both src and test directories and should store all the class files in the bin directory.">
    <javac destdir="bin" includes="*.java" classpathref="lib.path.id.junit.jar" includeantruntime="true" debug="true" >
        <src path="src" /> 
        <src path="test" />
    </javac>

</target>

Upvotes: 0

Yogendra Singh
Yogendra Singh

Reputation: 34387

Try including your junit jar file directly. Also make sure that the JAR file is available at the location.

<javac 
   srcdir="." 
   destdir="build/classes" 
   debug="true" 
   includeantruntime="true"
   classpath="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib/junit.jar"
 />

Or define a path element as :

 <path id="lib.path.id">
    <fileset dir="c:/eclipse/plugins/org.apache.ant_1.8.3.v20120321-1730/lib" 
             includes="junit.jar"/>
 </path>

and use the same in the javac as

<javac 
   srcdir="." 
   destdir="build/classes" 
   debug="true" 
   includeantruntime="true"
   classpathref="lib.path.id""
 />

Hope this helps.

Upvotes: 0

Dan Benamy
Dan Benamy

Reputation: 837

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>

Upvotes: 2

Related Questions