简体   繁体   中英

How do I make the JDK the default JRE?

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).


Edit: I ended up figuring out how to make the javac task use the Eclipse compiler (ecj.jar), which works very nicely.


Edit: Maven also supports using the Eclipse compiler, but this appears to be very rarely used and with an old version of ecj.jar. I intend to look in to this at a later time.


Edit: Using ecj with maven-compiler-plugin 3.0 works very well, and allows for building with a JRE.


Edit: I had problems with the javadoc tool crashing when parsing bytecode generated by ecj.

The answer is "no," there is no way to get the JDK to be the default JVM upon install.

As the other answers point out, you can adjust your path and your JAVA_HOME to point to the JDK, or a different JVM entirely. This is in fact what the Java installation does in the first place.

However, your problem is that you want tools.jar to be found. To do this you can copy it to the ext directory under your default JVM. Check the JDK file structure here . This will probably work.

On the other hand, if modifying the JAVA_HOME and PATH variables for Java seems annoying, remember that it's just one of a series of things we do to keep us sharp 开玩笑,糟透了,我们仍然需要在2009年这样做

  1. Download JDK from the website
  2. Once everything is finished, go to Control Panel
  3. Open JAVA
  4. Click on the Java tab and select View
  5. There will be one item present in the list. Change the Java Path from JRE to the JDK you downloaded, like so: C:\\Program Files\\Java\\<your_jdk_version>\\bin\\java.exe .
    For example, mine looks like this: C:\\Program Files\\Java\\jdk1.7.0_07\\bin\\java.exe

Copying the tools.jar file to a location where Eclipse is looking for it may work, but is messy and fragile since that's a step you may not remember the next time you upgrade your JDK. Better is to convince Eclipse to look for it in the proper location.

Setting JAVA_HOME to the correct location works for some tools, but Eclipse does not honor it .

A couple of things to try:

  • Make sure your JDK is identified and selected under Preferences->Java->Installed JREs .

  • Make sure Ant is being invoked by the JDK. One clue is that at the top of the Console output you should see the path of the javaw.exe which is being used. If that path is in the JRE, more convincing is needed. Check Run->External Tools->External Tools Configurations->[your Ant build]->JRE and make sure the settings there point to the JDK.

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 .

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.)

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 .

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 ).

Of cause this is doable. Not understand why people said No to this question. Copy over the tools.jar is not a wise way, I had the issue when using eclipese, Just add the jdk instead of jre to my build path and it works.

Here is the details:

http://www.gamefromscratch.com/post/2011/11/15/Telling-Eclipse-to-use-the-JDK-instead-of-JRE.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM