views:

2949

answers:

5

I use Eclipse with ant scripts, and Eclipse works well with the default JRE installation on Windows XP.

The annoyance comes when I want to run ant scripts compiling with the javac-tag, where it fails because there is no tools.jar in the classpath.

I've gotten the idea that if I could make the JDK become the default Java on Windows, then I would have what I have today, plus ant working out of the box.

Can this be done? What have I missed in the installation process?


Edit: I know of JAVA_HOME, but that is tedious and error prone (manually updating environment variables when a fresher JDK is available is not always something I remember).

+1  A: 

Try changing the JAVA_HOME environment variable to point to the JDK instead of the JRE.

Alternatively or possibly additionally, add a PATH entry to the JDK bin directory before any of the Windows system directories.

I suspect JAVA_HOME is enough to get Ant working, but it's sometimes nice to get the JDK version of java etc on the path too, so that when you just run java from the command line, you'll get exactly the same version as when you run javac.

Jon Skeet
Thanks for the workaround, which I know of. I'm just very tired of maintaining these pointers if a simple installation tweak could do the trick.
Thorbjørn Ravn Andersen
A: 

This is normally done by setting your JAVA_HOME environment variable to the root JDK directory that you want to use.

For example from a command line or batch file you simply do something like:

set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_14

However to set JAVA_HOME permanently add it to the environment variables on the Advanced tab in the property sheet for My Computer.

Nick Holt
+1  A: 
Yar
A: 

You could probably write a WSH script to reconfigure your path automatically.

This JScript script just prints some info:

//file:  jdk.js              usage: cscript /Nologo jdk.js
var objShell = WScript.CreateObject("WScript.Shell");
function setJdk(version) {
  try {
    var jdk = objShell.RegRead("HKEY_LOCAL_MACHINE\\SOFTWARE\\" +
       "JavaSoft\\Java Development Kit\\" + version + "\\JavaHome");
    if(jdk != null && jdk != "") {
      jdk += "\\bin";
      var path = objShell.RegRead("HKEY_CURRENT_USER\\Environment\\Path");
      path = jdk + ";" + path;
      WScript.Echo("Could set PATH to " + path + "\n");
    }
  } catch(err) { /*probably key does not exist*/ }
}
setJdk("1.7");
setJdk("1.6");
setJdk("1.5");

There is a RegWrite method that can be used to write to the registry. There is a bit of work involved determining the latest version and rejigging the path, removing obsolete entries and sorting out system versus user paths. No doubt it could be used to set JAVA_HOME too. The JDK entry would need to appear before the system32 directory, as the JRE installer puts a java.exe in there.

(I'm not 100% sure if the shell would pick up on such changes or whether a reboot/env variable reload of some kind would be required.)

McDowell
A: 

Apparently Eclipse can compile without the tools.jar. My guess it that they have a specific javac command that can work with a JRE (and not a JDK); this could be the reason why they can list their warnings.

Anyway, I would go the standard way (as suggested above) and install a JDK on your system. You can even start Eclipse with that specific JDK (without any JAVA_HOME change) by tweaking the eclipse.ini file (see these instructions).

Vladimir
Yes, Eclipse has its own incremental compiler. It either is Jikes or it is based on it (from what I recall).
cjstehno