Java 如何使 JDK 成为默认的 JRE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1388690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make the JDK the default JRE?
提问by Thorbj?rn Ravn Andersen
I use Eclipse with ant scripts, and Eclipse works well with the default JRE installation on Windows XP.
我将 Eclipse 与 ant 脚本一起使用,并且 Eclipse 可以很好地与 Windows XP 上的默认 JRE 安装配合使用。
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.
当我想运行使用 javac-tag 编译的 ant 脚本时,麻烦就来了,因为类路径中没有 tools.jar 导致它失败。
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.
我已经有了一个想法,如果我能让 JDK 成为 Windows 上的默认 Java,那么我就会拥有今天所拥有的东西,再加上开箱即用的 ant。
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).
编辑:我知道 JAVA_HOME,但这很乏味且容易出错(当有更新的 JDK 可用时手动更新环境变量并不总是我记得的事情)。
Edit: I ended up figuring out how to make the javac task use the Eclipse compiler (ecj.jar), which works very nicely.
编辑:我最终弄清楚如何使 javac 任务使用 Eclipse 编译器(ecj.jar),它工作得非常好。
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.
编辑:Maven 还支持使用 Eclipse 编译器,但这似乎很少使用,并且使用旧版本的 ecj.jar。我打算稍后再研究一下。
Edit: Using ecj with maven-compiler-plugin 3.0 works very well, and allows for building with a JRE.
编辑:将 ecj 与 maven-compiler-plugin 3.0 一起使用效果很好,并允许使用 JRE 进行构建。
Edit: I had problems with the javadoc tool crashing when parsing bytecode generated by ecj.
编辑:我在解析 ecj 生成的字节码时遇到了 javadoc 工具崩溃的问题。
采纳答案by Dan Rosenstark
The answer is "no," there is no way to get the JDK to be the default JVM upon install.
答案是否定的,无法在安装时让 JDK 成为默认的 JVM。
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.
正如其他答案所指出的那样,您可以调整路径和 JAVA_HOME 以指向 JDK 或完全不同的 JVM。这实际上是 Java 安装首先要做的。
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.
但是,您的问题是您希望找到 tools.jar。为此,您可以将其复制到默认 JVM 下的 ext 目录。在此处检查 JDK 文件结构。这可能会奏效。
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
另一方面,如果修改 Java 的 JAVA_HOME 和 PATH 变量看起来很烦人,请记住这只是我们为保持敏锐而做的一系列事情之一
回答by Jon Skeet
Try changing the JAVA_HOME environment variable to point to the JDK instead of the JRE.
尝试更改 JAVA_HOME 环境变量以指向 JDK 而不是 JRE。
Alternatively or possibly additionally, add a PATH entry to the JDK bin directory beforeany of the Windows system directories.
或者或可能另外,在任何 Windows 系统目录之前向 JDK bin 目录添加一个 PATH 条目。
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 exactlythe same version as when you run javac
.
我怀疑 JAVA_HOME 足以让 Ant 工作,但有时也可以在路径上获取 Java 等的 JDK 版本,这样当您从命令行运行时java
,您将获得与运行时完全相同的版本javac
.
回答by Nick Holt
This is normally done by setting your JAVA_HOME
environment variable to the root JDK directory that you want to use.
这通常是通过将JAVA_HOME
环境变量设置为要使用的根 JDK 目录来完成的。
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 Advancedtab in the property sheet for My Computer.
但是,要设置 JAVA_HOME 将其永久添加到我的电脑属性表中高级选项卡上的环境变量中。
回答by McDowell
You could probably write a WSH script to reconfigure your path automatically.
您可能会编写一个 WSH 脚本来自动重新配置您的路径。
This JScript script just prints some info:
这个 JScript 脚本只是打印一些信息:
//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 RegWritemethod 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.
有一个RegWrite方法可用于写入注册表。有一些工作涉及确定最新版本并重新调整路径,删除过时的条目并整理系统与用户路径。毫无疑问,它也可以用来设置JAVA_HOME
。JDK 条目需要出现在system32
目录之前,因为 JRE 安装程序java.exe
在那里放置了一个。
(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.)
(我不是 100% 确定 shell 是否会接受此类更改,或者是否需要重新加载某种类型的重启/环境变量。)
回答by Vladimir
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.
显然 Eclipse 可以在没有tools.jar
. 我猜他们有一个javac
可以与 JRE(而不是 JDK)一起使用的特定命令;这可能是他们可以列出警告的原因。
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).
无论如何,我会采用标准方式(如上所述)并在您的系统上安装 JDK。您甚至可以JAVA_HOME
通过调整eclipse.ini
文件(请参阅这些说明)使用该特定 JDK(无需任何更改)启动 Eclipse 。
回答by Lyle
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.
将 tools.jar 文件复制到 Eclipse 正在寻找它的位置可能会起作用,但它是混乱和脆弱的,因为这是您下次升级 JDK 时可能不记得的步骤。更好的是说服 Eclipse 在适当的位置寻找它。
Setting JAVA_HOME to the correct location works for some tools, but Eclipse does not honor it.
将 JAVA_HOME 设置为正确的位置适用于某些工具,但Eclipse 不支持它。
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]->JREand make sure the settings there point to the JDK.
确保在Preferences->Java->Installed JREs下识别并选择了您的 JDK 。
确保 JDK 正在调用 Ant。一个线索是,在控制台输出的顶部,您应该看到正在使用的 javaw.exe 的路径。如果该路径在 JRE 中,则需要更有说服力。检查 Run->External Tools->External Tools Configurations->[your Ant build]->JRE并确保那里的设置指向 JDK。
回答by charles
- Download JDK from the website
- Once everything is finished, go to Control Panel
- Open JAVA
- Click on the Java tab and select View
- 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
- 从网站下载JDK
- 一切都完成后,转到控制面板
- 打开JAVA
- 单击 Java 选项卡并选择查看
- 列表中将出现一项。改变从JRE到你下载JDK的Java的路径,像这样:
C:\Program Files\Java\<your_jdk_version>\bin\java.exe
。
例如,我的看起来像这样:C:\Program Files\Java\jdk1.7.0_07\bin\java.exe
回答by user3610798
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.
因为这是可行的。不明白为什么人们对这个问题说不。复制 tools.jar 不是一个明智的方法,我在使用 eclipese 时遇到了问题,只需将 jdk 而不是 jre 添加到我的构建路径中,它就可以工作。
Here is the details:
以下是详细信息:
http://www.gamefromscratch.com/post/2011/11/15/Telling-Eclipse-to-use-the-JDK-instead-of-JRE.aspx
http://www.gamefromscratch.com/post/2011/11/15/Telling-Eclipse-to-use-the-JDK-instead-of-JRE.aspx