• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

<junit> problem with Ant

Ranch Hand
Posts: 511
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,

I have an Ant build script where I placed the following target:

Now, when I run this target by using "ant tests", this is what I get in response:


F:\DevProjects\JavaProjects\sellcars\build.xml:51: Could not create task or type of type: junit.

Ant could not find the task or a class this task relies upon.

This is common and has a number of causes; the usual
solutions are to read the manual pages then download and
install needed JAR files, or fix the build file:
- You have misspelt 'junit'.
Fix: check your spelling.
- The task needs an external JAR file to execute
and this is not found at the right place in the classpath.
Fix: check the documentation for dependencies.
Fix: declare the task.
- The task is an Ant optional task and the JAR file and/or libraries
implementing the functionality were not found at the time you
yourself built your installation of Ant from the Ant sources.
Fix: Look in the ANT_HOME/lib for the 'ant-' JAR corresponding to the
task and make sure it contains more than merely a META-INF/MANIFEST.MF.
If all it contains is the manifest, then rebuild Ant with the needed
libraries present in ${ant.home}/lib/optional/ , or alternatively,
download a pre-built release version from apache.org
- The build file was written for a later version of Ant
Fix: upgrade to at least the latest release version of Ant
- The task is not an Ant core or optional task
and needs to be declared using <taskdef>.
- You are attempting to use a task defined using
<presetdef> or <macrodef> but have spelt wrong or not
defined it at the point of use

Remember that for JAR files to be visible to Ant tasks implemented
in ANT_HOME/lib, the files must be in the same directory or on the

Please neither file bug reports on this problem, nor email the
Ant mailing lists, until all of these causes have been explored,
as this is not an Ant bug.



(1) What am I possibly doing wrong? Under my ANT_HOME/lib directory, a junit.jar existed...

In addition, in this particular line:

(2) How do I specify where my TestShoppingCart test case is residing? I mean, if I set a property to ${test.dir} directory, right underneath my project's root, would this work?

Many, many thanks!
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been struggling with the same thing myself.
Apparently, it's a ClassLoader issue, and it's made worse by the fact that they've changed the class loading policy between Ant 1.5.x and 1.6.x....

Apache have a FAQ on this (see list below).
One thing that worked for me (on Ant 1.6.5) was to call
> ant -lib SOME_DIR/junit-3.8.1.jar
When you run it this way, the junit jar should *not* appaer in the < classpath > element inside the build.xml file. It also should *not* appear in ANT_HOME/lib.

Couple of useful links:


Saloon Keeper
Posts: 25614
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a royal pain is what it is!

There are two fairly simple solutions.

1. Include junit in the CLASSPATH in effect when you invoke Ant. I don't like this because it's extra meta-information that I wilol sooner or late forget to supply when I need it most.

2. Drop a copy of the junit.jar into ANT_HOME/lib, thereby ensuring that it will be part of Ant's classpath at all times (or at least as long as you use that copy of Ant).

Since you also need junit.jar in the javac classpath, you can use case 2 and specify <pathelement location="${ant.home}/lib/junit.jar"/> as part of the test-compile classpath. Or something like that, at any rate.
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic