«« Blogging Might Get You Fired RSS is doesn't scale? Numbers please. »»
blog header image
<junit> Ant task doesn't work in Eclipse

Here's another solution to a problem I'm hoping to get Googled: If you're running an Ant build.xml file in Eclipse, the <junit> task will not work by default. If you attempt to use the <junit> task with a fresh install of Eclipse, you will receive the following error message:

BUILD FAILED: [ECLIPSE_DIR]\workspace\[PROJECT_DIR]\build.xml:208: 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 optional.jar is absent
Fix: look for optional.jar in ANT_HOME/lib, download if needed
- The task was not built into optional.jar as dependent
libraries were not found at build time.
Fix: look in the JAR to verify, then rebuild with the needed
libraries, or download a 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>.

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
classpath

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.

The <junit> Ant task is an optional task and this is the standard Ant error message when Ant cannot find the supporting code for an optional task. Ant includes support for the tasks themselves, but not the internal code that actually does the work.

So you need to find a copy of junit.jar, the code that does JUnit work, and tell Ant where it is. The easiest way to do this is to go to Window --> Preferences, Ant --> Runtime. In the Classpath tab click on Global Entries and then Add External JARs....

Eclipse already has a copy of JUnit, so in the dialog find your Eclipse plugins directory ([ECLIPSE_DIR]/plugins/) and go to the JUnit plugin (presently org.junit_3.8.1) and select junit.jar. Now all of your Ant scripts will know how to do the <junit> Ant task.

Posted at September 08, 2004 at 08:27 AM EST
Last updated September 08, 2004 at 08:27 AM EST
Comments

Great!! This was just the information I was looking for!

» Posted by: Oliver at October 8, 2004 12:50 PM

Argh. Been struggling with an 'annoyance' since yesterday.

I managed to figure this part out myself (I used an external Ant instead and pointed Eclipse to that).

What's royally annoying me now is this: my JUnit test code runs fine when run in Eclipse ("Run as... JUnit").

It also runs fine when ran through the Ant from the command-line.

It screws up when Ant is ran from within Eclipse. But just the test suite - the "compile-all" targets run fine either way.

I've somewhat narrowed it down to a classpath difference when running Ant one way or the other, but was wondering if you or anyone else reading this has had a similar experience and knows what's going on?

» Posted by: Alistair Israel at October 18, 2004 11:26 PM

I've had the same problems with the classpath as above but again managed to figure it out.
What i found was that to successfully run the test case from within the build.xml i had to add the path to the source directory (of the java classes being tested) and the path of the test cases to the classpath within the junit task.

One annoying thing i also found, was that i had to add the junit.jar and the ant-junit.jar to the eclipse preferences for them to get picked up. Adding them to the classpath for the taskdef or junit tasks simply did not work.

» Posted by: Sunjay at October 20, 2004 01:12 PM

To quote Austin Powers: "Yeah, baby!"

I read the Ant FAQ (see URL), or at least tried to. This particular entry screams for an editor.

» Posted by: Jeff Lowery at November 1, 2004 02:10 PM

Thanks ! This way works !

» Posted by: fx at November 12, 2004 04:01 AM

Thanks! Real annoying problem!

» Posted by: Jonas at November 12, 2004 06:39 AM

Listen, you just HAVE to add this to the official ant docs! That's such a pain in the ass! I thought I could add junit.jar to the classpath using some tag like <project... classpath="/someshere/junit.jar"/> but I couldn't make it through! Your article was of great help for me! Thanks!
Igor

» Posted by: at November 13, 2004 06:14 AM

That is great !but i have even not know how to use ant ,what a poor man!

» Posted by: qingyanLiu at November 18, 2004 09:06 PM

Two months old and this post is still helping people -- saved me just tonight. ;) Thanks ...
-j

» Posted by: James at November 19, 2004 09:41 PM

Thanks mate, your article got me out of a jam.

» Posted by: Damian Sutton at January 10, 2005 05:53 PM

Thanks! This way works !

» Posted by: bulain at January 10, 2005 09:09 PM

Praise be to Google and bloggers like you!

» Posted by: Wikus Hartzenberg at January 19, 2005 04:39 PM

thanks a lot, a lot!!!... you just saved a poor french womanwho was trying to make Eclipse work with this ....

» Posted by: Mariani Patricia at January 21, 2005 11:51 AM

i will name my first child after you!!!!!

thanks!

» Posted by: mike sandman at January 31, 2005 11:20 PM

I am screaming!Great!!

» Posted by: darren at February 17, 2005 08:48 AM

but what's the use of ant-junit.jar in ANT HOME entries as to Gloabal Entires?

» Posted by: darren at February 17, 2005 08:54 AM

Aww yes! problem solved, thanks for the post

» Posted by: Fraser at February 21, 2005 07:31 AM

I have the same problem, but no solution. I'm using Ant 1.6. ANT_HOME is defined, and I put junit.jar in the ANT_HOME/lib, and the file, ant-junit.jar, is already there. It still doesn't work! What a pain!

» Posted by: Jim at February 21, 2005 11:17 AM

Jim, please read this blog post again. Eclipse ignores ANT_HOME. You need to get junit.jar on the runtime classpath of Ant that Eclipse uses --> the Ant plugin.

Don't set ANT_HOME. Don't move junit.jar. Eclipse won't care. Just follow the instructions I have here. :)

» Posted by: Ryan at February 21, 2005 11:23 AM

Sorry, I should've been more specific. I'm running Ant from the command line, not Eclipse. I know, I know...not exactly what this forum is about, but hey, I gotta ask somebody. This is taking too much company time to figure out.

» Posted by: Jim at February 21, 2005 11:56 AM

Of course, as soon as I ask...

It seems our ant-junit.jar doesn't contain any class files. I'm not sure why this is...perhaps an installation flag that builds them with classes or leaves them (nearly) empty. Hopefully posting wasn't a complete waste. Perhaps another Googler with the same problem will stumble upon this.

» Posted by: jim at February 21, 2005 12:36 PM

Just for reference, the ant-junit.jar file in the version of Ant I use (1.6.2) has 30 class files, 2 XSL files and an MF file for a total of 33 files.

» Posted by: Ryan at February 21, 2005 12:40 PM

brilliant. cheers for that!

» Posted by: powlow at February 22, 2005 10:50 AM

Thanks for the help. Ant's documentation, as pointed out earlier, is very unclear.

» Posted by: jer at February 22, 2005 04:36 PM

Brilliant! Thank you!

» Posted by: RJ at February 23, 2005 01:41 PM

i've been looking for this all day!! thanx

» Posted by: rocksteady at March 4, 2005 07:08 AM

Thank you!!! Thank you!!! Thank you!!!

» Posted by: Mike De Haan at March 10, 2005 02:07 PM

Thank you ! Works fine !

» Posted by: Danny at March 11, 2005 12:09 PM

Thanks a lot
That stupid thing has taken hours of my job time. I don't know if it's a lack of junit documentation pages or Eclipse fault, but this little-n-anoying-thing should be written anywhere!!

» Posted by: paxtranslator at March 14, 2005 05:02 AM

Sadly, I have the same problem on the ant command line. On our build machine, I have put the junit-3.8.1.jar in the ANT_HOME/lib dir, ANT_HOME is defined, and so forth. But for some reason, ant STILL can not find this!! Is there some reason this may occur at runtime from the command line? I look in the ENV and ANT_HOME, JAVA_HOME, etc are all defined. IS there another file that junit depends on that I am missing? What about junitreport task? You'd think by now since its used so widely this would be defaulted when you get ant. Junit is open-source, so it can be distributed with ant. Not sure why they don't just bundle it directly.

» Posted by: Kevin at March 15, 2005 09:43 PM

Thanks for the great post. Graphics would make it even easier for us "slow" types.

» Posted by: SteSkilz at March 30, 2005 03:45 PM

Chalk up another satisfied customer. Fortunately I've gotten in the habit of Googling first when I come upon a problem, so it took me but moments to fix this. Thanks!

» Posted by: Thomas at April 3, 2005 01:35 PM

Thanks!! Thanks !! Thanks!!

» Posted by: Alvin at April 13, 2005 07:03 AM

First class, Ryan. This is one of those weird goofs that can *really* slow one down. Nice job.

» Posted by: El Guapo at April 20, 2005 07:32 PM

thanks for the help man!

» Posted by: Edans Sandes at April 21, 2005 12:58 PM

Gah! This was causing me serious grief. Thanks for the good pointer.

» Posted by: Evan Prodromou at April 22, 2005 12:20 PM

Thanks! It solved my problem.

» Posted by: Jorge at April 27, 2005 10:51 AM

Thanks a lot! this solved an issue I have been struggling with for several hours!

» Posted by: carlo at April 27, 2005 11:17 AM
Google
 
Search scope: Web ryanlowe.ca