Trouble with Apache Ant Tutorial - JUnit

I'm trying to following apache's ANT tutorial:

I'm currently at the JUnit portion, but I keep getting errors related to not being able to find the junit.framework package.

I believe I've followed the tutorial exactly as follows - Here are the details:

-----My file structure-----




Main-Class: oata.HelloWorld


<project name ="HelloWorld" basedir="." default="main">
    <property name="src.dir" value="src" />
    <property name="build.dir" value="build" />
    <property name="classes.dir" value="${build.dir}/classes" />
    <property name="jar.dir"    value="${build.dir}/jar" />
    <property name="lib.dir"    value="lib" />

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />

    <property name="main-class" value="oata.HelloWorld" />

    <target name="clean">
        <delete dir="${build.dir}"/>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" />
        <copy todir="${classes.dir}" >
            <fileset dir="${src.dir}" excludes="**/*.java" />

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}" />
        <jar destfile="${jar.dir}/${}.jar" basedir="${classes.dir}">
                <attribute name="Main-Class" value="${main-class}" />

    <target name="run" depends="jar">
        <java fork="true" classname="${main-class}">
                <path refid="classpath"/>
            <path id="application" location="${jar.dir}/${}.jar"/>

    <target name="junit" depends="jar">
            <junit printsummary="yes">
                    <path refid="classpath"/>
                    <path refid="application"/>

                <batchtest fork="yes">
                    <fileset dir="${src.dir}" includes="*"/>

    <target name="clean-build" depends="clean,jar" />

    <target name="main" depends="clean,run" />

public class HelloWorldTest extends junit.framework.TestCase {

    public void testNothing() {

    public void testWillAlwaysFail() {
        fail("An error message");


package oata;

import org.apache.log4j.Logger;

public class HelloWorld {
    static Logger logger = Logger.getLogger(HelloWorld.class);

    public static void main(String [] args){"Hello World!");

log4j.rootLogger=DEBUG, stdout



 ant -verbose junit
Apache Ant(TM) version 1.8.2 compiled on June 20 2012
Trying the default build file: build.xml
Buildfile: /Users/jtyler/Projects/AntHelloWorld/build.xml
Detected Java version: 1.6 in: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Detected OS: Mac OS X
parsing buildfile /Users/jtyler/Projects/AntHelloWorld/build.xml with URI = file:/Users/jtyler/Projects/AntHelloWorld/build.xml
Project base dir set to: /Users/jtyler/Projects/AntHelloWorld
parsing buildfile jar:file:/usr/share/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/usr/share/ant/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file
Build sequence for target(s) `junit' is [compile, jar, junit]
Complete build sequence is [compile, jar, junit, clean, run, main, clean-build, ]

    [mkdir] Skipping /Users/jtyler/Projects/AntHelloWorld/build/classes because it already exists.
    [javac] /Users/jtyler/Projects/AntHelloWorld/build.xml:20: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] added as HelloWorldTest.class doesn't exist.
    [javac] /Users/jtyler/Projects/AntHelloWorld/src/ skipped - don't know how to handle it
    [javac] oata/ added as oata/HelloWorld.class doesn't exist.
    [javac] Compiling 2 source files to /Users/jtyler/Projects/AntHelloWorld/build/classes
    [javac] Using modern compiler
    [javac] Compilation arguments:
    [javac] '-d'
    [javac] '/Users/jtyler/Projects/AntHelloWorld/build/classes'
    [javac] '-classpath'
    [javac] '/Users/jtyler/Projects/AntHelloWorld/build/classes:/Users/jtyler/Projects/AntHelloWorld/lib/log4j-1.2.8.jar:/usr/share/ant/lib/ant-launcher.jar:/usr/share/ant/lib/ant-antlr.jar:/usr/share/ant/lib/ant-jmf.jar:/usr/share/ant/lib/ant-junit.jar:/usr/share/ant/lib/ant-junit4.jar:/usr/share/ant/lib/ant-swing.jar:/usr/share/ant/lib/ant-testutil.jar:/usr/share/ant/lib/ant.jar'
    [javac] '-sourcepath'
    [javac] '/Users/jtyler/Projects/AntHelloWorld/src'
    [javac] '-g:none'
    [javac] The ' characters around the executable and arguments are
    [javac] not part of the command.
    [javac] Files to be compiled:
    [javac]     /Users/jtyler/Projects/AntHelloWorld/src/
    [javac]     /Users/jtyler/Projects/AntHelloWorld/src/oata/
    [javac] /Users/jtyler/Projects/AntHelloWorld/src/ package junit.framework does not exist
    [javac] public class HelloWorldTest extends junit.framework.TestCase {
    [javac]                                                    ^
    [javac] /Users/jtyler/Projects/AntHelloWorld/src/ cannot find symbol
    [javac] symbol  : method fail(java.lang.String)
    [javac] location: class HelloWorldTest
    [javac]         fail("An error message");
    [javac]         ^
    [javac] 2 errors

/Users/jtyler/Projects/AntHelloWorld/build.xml:20: Compile failed; see the compiler error output for details.
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(
    at java.lang.reflect.Method.invoke(

Total time: 0 seconds

Is there something I'm missing that's needed to be included that was (or wasn't) mentioned in the tutorial?

EDIT: Based on Alex's inquiries, I tested to see if I had ant's junit jars in my shared directories (which I do):

$ ls /usr/share/ant/lib/ant-junit*.jar
/usr/share/ant/lib/ant-junit.jar    /usr/share/ant/lib/ant-junit4.jar

I also tried to manually put ant-junit, ant-junit4.jar, and junit.jar into my project's lib directory, with the following results (all errors) after running ant clean junit:

  • junit.jar - /Users/jtyler/Projects/AntHelloWorld/build.xml:45: Reference application not found.
  • ant-junit.jar: package junit.framework does not exist
  • ant-junit.jar and ant.junit4.jar: package junit.framework does not exist
  • ant-junit4.jar: package junit.framework does not exist

EDIT The whole error when only junit.jar is included in the lib folder and calling ant clean junit is:

$ ant clean junit
Buildfile: /Users/jtyler/Projects/AntHelloWorld/build.xml

   [delete] Deleting directory /Users/jtyler/Projects/AntHelloWorld/build

    [mkdir] Created dir: /Users/jtyler/Projects/AntHelloWorld/build/classes
    [javac] /Users/jtyler/Projects/AntHelloWorld/build.xml:20: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 2 source files to /Users/jtyler/Projects/AntHelloWorld/build/classes
     [copy] Copying 1 file to /Users/jtyler/Projects/AntHelloWorld/build/classes

    [mkdir] Created dir: /Users/jtyler/Projects/AntHelloWorld/build/jar
      [jar] Building jar: /Users/jtyler/Projects/AntHelloWorld/build/jar/HelloWorld.jar


/Users/jtyler/Projects/AntHelloWorld/build.xml:45: Reference application not found.


The tutorial is misleading. The junit files that are included in ant do not work. I downloaded juinit and copied the jar file to the lib folder of the project and it now works fine.

On top of including the junit.jar file within my lib dir, I added the following line to my build.xml file right above the junit task declaration:

<path id="application" location="${jar.dir}/${}.jar"/>

which is a copy of the 'application' path id declaration within the 'run' task definition.

The application builds successfully, and I get the following output with ant junit (test failure is expected):

ant junit
Buildfile: /Users/jtyler/Projects/AntHelloWorld/build.xml

    [javac] /Users/jtyler/Projects/AntHelloWorld/build.xml:20: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds


    [junit] Running HelloWorldTest
    [junit] Tests run: 2, Failures: 1, Errors: 0, Time elapsed: 0.001 sec
    [junit] Test HelloWorldTest FAILED

Total time: 1 second

Comments the following questions would be greatly appreciated:

  1. Is the fact I actually needed to include the junit.jar file in my lib folder something I should be concerned about? I would assume this is not necessary if ant's tutorial explicitly states "Because Ant has a built-in JUnit 3.8.2 you could start directly using it."

  2. Is the fact I needed to define the 'application' path id outside of the run task definition a typo within the apache ant tutorial? Or are there other things I missed or should consider?

Thanks to all those that helped.

You must specify junit.jar itself in your compile and in your <junit> task's classpath. This is not automatically included in your compile:

<!--  Contains junit.jar and other needed test classes -->
<path id="junit.classpath">
    <fileset dir="${}"/>

<!-- Contains third party jars your code is dependent upon -->
<path id="main.classpath">
    <fileset dir="${dependent.jar.dir}"/>

<!-- Your main Java source code directory -->
<property name="main.srcdir" value="${basedir}/src/main/java"/>

<!-- Your main Junit test directory source code -->
<property name="test.srcdir" value="${basedir}/src/test/java"/>

<!-- Where you're compiling your main classes to -->
<property name="main.destdir" value="${basedir}/target/classes"/>

<!-- Where you're compiling your test classes to -->
<property name="test.destdir" value="${basedir}/target/test-classes"/>

<!-- Compile Test Classes -->
<!-- Notice you have three classpath elements:
    * Your main classes you compiled before
    * Your test main classpath that your main classes
      were dependent up
    * The junit.jar classpath

<javac destdir="${test.destdir}"
          <!-- Your non-test classes you compiled before -->
          <pathelement path="${main.destdir}/> 
      <classpath refid="junit.classpath"/>
      <classpath refid="main.classpath"/>

<!-- Now run your unit tests: Note how you have the same
     three classpath elements as before -->

 <junit fork="yes">
         <pathelement path="${main.destdir}"/>
         <pathelement path="${test.destdir}"/>
      <classpath refid="main.classpath"/>
      <classpath refid="junit.classpath"/>

Need Your Help

Log4j 2 doesn't support file anymore?

java log4j log4j2

I am running an example using log4j 2.0-rc1 and file, but log4j lib always runs it with the default configuration (log level, appender, etc). I also tried changing the name to log4j2.

Visual Studio Code: Cannot find name angular?

javascript angularjs visual-studio-code

I have an Angular App and it works fine, but my debugger is showing me 'Cannot find name angular'.