Ant within Eclipse: switching JDKs and finding tools.jar


I've been doing quite a lot of work creating new Ant build processes and grokking Eclipse (installing and reinstalling on different machines), and this is a problem that keeps recurring. This morning I cleaned up vast amounts of garbage on my main Windows machine, garbage left over from old J2SDK installs (I had FOUR--when will Sun fix the install problem?) and I reinstalled JDK 1.4.2_03 and then tried running everything again within Eclipse (v3.0 M7). Needless to say, Ant was running fine before, after I had pointed to tools.jar but now that I had changed JDKs it wasn't guaranteed that it would run--and it didn't. While it is possible that this is so well known that people do it without thinking, it certainly isn't clearly documented, and it's a situation that people will probably find regularly doing a clean install of Eclipse and the JDK on a machine, or when upgrading JDKs after the settings have been done long ago--and forgotten. :)

First, the situation. On restart, Eclipse correctly detected the new JRE (clearly from the registry entries created by the JDK/JRE install) to the one the JSDK installs in C:\Program Files\Java\... but it's better to change the pointer to the JRE within the JDK IMO. Even then, Ant doesn't work. The error message you get is for Ant:

[javac] BUILD FAILED: [build file location here]
Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK

Of course, JAVA_HOME is pointing to the right location, in both the OS environment and within Eclipse (This variable can be set within Eclipse through Window > Preferences > Java > Classpath Variables).

So how to fix the Ant build problem?

I found various solutions searching, for example running Eclipse with "eclipse -vm [JDKPATH]\bin" but that didn't quite satisfy me (I wanted something that could be configured within the environment). Other solutions to the problem where even more esoteric.

The best solution I've found (after a little bit of digging through Eclipse's options) is to edit Ant's runtime properties. Go to Window > Preferences > Ant > Runtime. Choose the Classpath tab. Select the Global Properties node in the tree and click Add External JARs. Select tools.jar from your JDK directory (e.g., j2sdk1.4.2_03\lib\tools.jar). Click Apply and OK, and you should be on your way. Not too hard when you know what to do. Now if this could only be done automatically by Eclipse on install...

Categories: soft.dev
Posted by diego on February 22 2004 at 4:26 PM
Comments (please see the comments & trackback policy).

Perhaps not a solution if you're using Eclipse, but to have Ant work with multiple JDK's I just have different (on Windows) .bat files that set the various JAVA_HOME values. For example, I have D:\tools\ant\bin in my PATH, and inside it I have

ant.bat
ant122.bat
ant141.bat
ant142.bat

Each one differs from the other only at the beginning lines, for example ant142.bat has

rem set java home for jdk 1.4.2
set JAVA_HOME=C:\Programmi\JDK142

Hope this helps.

Posted by: Sebastiano Pilla at February 23, 2004 10:09 AM

Yeah,

I had the same problem and found the same solution http://www.mjwall.com/node/view/134

Seems like it would be easy to fix, but still doesn't work in 3.0M6

Posted by: Mike Wall at February 23, 2004 6:36 PM

An even better solution is:

In Window->Preferences->Java->Installed JREs:

Open your JRE and deselect "Use default system libraries". Now add the location of tools.jar to that classpath. Make sure you are using the SDK and not the JRE.

Now when you change the JRE for a particular Ant build, it will add the correct tools.jar to the classpath after you click "Now".

Posted by: John Schult at February 25, 2004 6:58 PM

Copyright © Diego Doval 2002-2007.
Powered by
Movable Type 3.35