如何设置默认 Java 安装/运行时 (Windows)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/551668/
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 set the default Java installation/runtime (Windows)?
提问by Stephen
I'm in the situation where I've installed the JDK, but I can't run applets in browsers (I may not have installed the JRE).
我的情况是我已经安装了 JDK,但是我无法在浏览器中运行小程序(我可能没有安装 JRE)。
However, when I install the JRE, it clobbers my JDK as the default runtime. This breaks pretty much everything (Eclipse, Ant) - as they require a server JVM.
但是,当我安装 JRE 时,它破坏了我的 JDK 作为默认运行时。这几乎破坏了一切(Eclipse、Ant)——因为它们需要一个服务器 JVM。
There's no JAVA_HOME
environment variable these days - it just seems to use some registry magic (setting the system path is of no use either). Previously, I've just uninstalled the JRE after I've used it to restore the JDK. This time I want to fix it properly.
JAVA_HOME
这些天没有环境变量 - 它似乎只是使用了一些注册表魔法(设置系统路径也没有用)。以前,我在使用 JRE 恢复 JDK 后刚刚卸载了它。这次我想正确修复它。
This also manifests itself with the jre autoupdater - once upon a time, I had a working setup with the JDK and JRE, but it updated and bust everything.
这也体现在 jre 自动更新程序上——曾几何时,我有一个使用 JDK 和 JRE 的工作设置,但它更新并破坏了一切。
采纳答案by nbeyer
This is a bit of a pain on Windows. Here's what I do.
这在 Windows 上有点痛苦。这就是我所做的。
Install latest Sun JDK, e.g. 6u11, in path like c:\install\jdk\sun\6u11
, then let the installer install public JRE in the default place (c:\program files\blah
). This will setup your default JRE for the majority of things.
在 like 路径中安装最新的 Sun JDK,例如6u11,c:\install\jdk\sun\6u11
然后让安装程序在默认位置 ( c:\program files\blah
)安装公共 JRE 。这将为大多数事情设置您的默认 JRE。
Install older JDKs as necessary, like 5u18in c:\install\jdk\sun\5u18
, but don't install the public JREs.
根据需要安装较旧的 JDK,例如5u18in c:\install\jdk\sun\5u18
,但不要安装公共 JRE。
When in development, I have a little batch file that I use to setup a command prompt for each JDK version. Essentially just set JAVA_HOME=c:\jdk\sun\JDK_DESIRED
and then set PATH=%JAVA_HOME%\bin;%PATH%
. This will put the desired JDK first in the path and any secondary tools like Ant or Maven can use the JAVA_HOME
variable.
在开发过程中,我有一个小批处理文件,用于为每个 JDK 版本设置命令提示符。基本上只是设置JAVA_HOME=c:\jdk\sun\JDK_DESIRED
然后设置PATH=%JAVA_HOME%\bin;%PATH%
。这会将所需的 JDK 首先放在路径中,并且任何辅助工具(如 Ant 或 Maven)都可以使用该JAVA_HOME
变量。
The path is important because most public JRE installs put a linked executable at c:\WINDOWS\System32\java.exe
, which usually overridesmost other settings.
该路径很重要,因为大多数公共 JRE 安装将链接的可执行文件放在c:\WINDOWS\System32\java.exe
,这通常会覆盖大多数其他设置。
回答by PhiLho
I have several JDK (1.4, 1.5, 1.6) installed in C:\Java
with their JREs. Then I let Sun update the public JRE in C:\Program Files\Java
.
Lately there is an improvement, installing in jre6. Previously, there was a different folder per new version (1.5.0_4, 1.5.0_5, etc.), which was taking lot of space
我安装了几个C:\Java
带有 JRE 的JDK(1.4、1.5、1.6)。然后我让 Sun 在C:\Program Files\Java
.
最近有一个改进,安装在 jre6 中。以前,每个新版本(1.5.0_4、1.5.0_5 等)都有一个不同的文件夹,占用大量空间
回答by J?rn Guy Sü?
I have patched the behaviour of my eclipse startup shortcut in the properties dialogue
我已经在属性对话框中修补了我的 eclipse 启动快捷方式的行为
from
从
"E:\Program Files\eclipse\eclipse.exe"
to
到
"E:\Program Files\eclipse\eclipse.exe" -vm "E:\Program Files\Java\jdk1.6.0_30\bin"
as described in the Eclipse documentation
It is a patch only, as it depends on the shortcut to fix things...
它只是一个补丁,因为它取决于修复问题的快捷方式......
The alternative is to set the parameter permanently in the eclipse initialisation file.
另一种方法是在 eclipse 初始化文件中永久设置参数。
回答by snovelli
After many attempts, I found the junction approach more convenient. This is very similar on how this problem is solved in linux.
经过多次尝试,我发现连接方式更方便。这与如何在 linux 中解决此问题非常相似。
Basically it consists of having a linkbetween c:\tools\java\default
and the actual version of java you want to use as default in your system.
基本上,它包括在您要在系统中用作默认值的实际 Java 版本之间建立链接c:\tools\java\default
。
How to set it:
如何设置:
- Download junctionand make sure to put it in your PATH environment variable
- Set your environment this way:
-
PATH
pointing to ONLYto this jrec:\tools\java\default\bin
-JAVA_HOME
pointing to `c:\tools\java\default - Store all your jre-s in one folder like (if you do that in your Program FIles folder you may encounter some
C:\tools\Java\JRE_1.6
C:\tools\Java\JRE_1.7
C:\tools\Java\JRE_1.8
- Open a command prompt and cd to
C:\tools\Java\
- Execute
junction default JRE_1.6
- 下载连接点并确保将其放在您的 PATH 环境变量中
- 以这种方式设置您的环境: -仅
PATH
指向此 jre -指向 `c:\tools\java\defaultc:\tools\java\default\bin
JAVA_HOME
- 将所有 jre-s 存储在一个文件夹中(如果您在 Program FIles 文件夹中这样做,您可能会遇到一些
C:\tools\Java\JRE_1.6
C:\tools\Java\JRE_1.7
C:\tools\Java\JRE_1.8
- 打开命令提示符并 cd 到
C:\tools\Java\
- 执行
junction default JRE_1.6
This will create a junction(which is more or less like a symbolic link in linux) between C:\tools\java\default
and C:\tools\java\JRE_1.6
这会创建一个结(这是或多或少像在linux符号链接)之间C:\tools\java\default
和C:\tools\java\JRE_1.6
In this way you will always have your default java in c:\tools\java\default
.
通过这种方式,您将始终在c:\tools\java\default
.
If you then need to change your default java to the 1.8 version you just need to execute
如果您需要将默认的 java 更改为 1.8 版本,您只需要执行
junction -d default
junction default JRE_1.8
Then you can have batch files to do that without command prompt like
set_jdk8.bat
set_jdk7.bat
然后你可以有批处理文件来做到这一点,而无需命令提示符
set_jdk8.bat
set_jdk7.bat
As suggested from @С?а??еМаят?и
正如@С?а??еМаят?и所建议的那样
EDIT: From windows vista, you can use mklink /J default JRE_1.8
编辑:从 windows vista,您可以使用 mklink /J default JRE_1.8
回答by John Mikic
I simply install all the versions of JDK I need and the latest installed becomes default, so I just reinstall the one I want to be default if necessary.
我只需安装我需要的所有 JDK 版本,最新安装的版本将成为默认版本,因此我只需重新安装我希望成为默认版本的版本(如有必要)。
回答by Betlista
I just had that problem (Java 1.8 vs. Java 9 on Windows 7) and my findings are:
我刚刚遇到了这个问题(Windows 7 上的 Java 1.8 与 Java 9),我的发现是:
short version
精简版
default seems to be (because of Path entry)
默认似乎是(因为路径条目)
c:\ProgramData\Oracle\Java\javapath\java -version
select the version you want (test, use tab completing in cmd, not sure what those numbers represent), I had 2 options, see longer version for details
选择你想要的版本(测试,在 cmd 中使用 tab 完成,不确定这些数字代表什么),我有 2 个选项,查看更长的版本了解详细信息
c:\ProgramData\Oracle\Java\javapath_target_[tab]
remove junction/link and link to your version (the one ending with 181743567 in my case for Java 8)
删除连接/链接并链接到您的版本(在我的 Java 8 中以 181743567 结尾的版本)
rmdir javapath
mklink /D javapath javapath_target_181743567
longer version:
更长的版本:
Reinstall Java 1.8 after Java 9 didn't work. The sequence of installations was jdk1.8.0_74, jdk-9.0.4 and attempt to make Java 8 default with jdk1.8.0_162...
在 Java 9 不起作用后重新安装 Java 1.8。安装顺序是 jdk1.8.0_74、jdk-9.0.4 并尝试使用 jdk1.8.0_162 使 Java 8 默认...
After jdk1.8.0_162 installation I still have
jdk1.8.0_162安装后我还有
java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
What I see in path is
我在路径中看到的是
Path=...;C:\ProgramData\Oracle\Java\javapath;...
So I checked what is that and I found it is a junction (link)
所以我检查了那是什么,我发现它是一个路口(链接)
c:\ProgramData\Oracle\Java>dir
Volume in drive C is OSDisk
Volume Serial Number is DA2F-C2CC
Directory of c:\ProgramData\Oracle\Java
2018-02-07 17:06 <DIR> .
2018-02-07 17:06 <DIR> ..
2018-02-08 17:08 <DIR> .oracle_jre_usage
2017-08-22 11:04 <DIR> installcache
2018-02-08 17:08 <DIR> installcache_x64
2018-02-07 17:06 <JUNCTION> javapath [C:\ProgramData\Oracle\Java\javapath_target_185258831]
2018-02-07 17:06 <DIR> javapath_target_181743567
2018-02-07 17:06 <DIR> javapath_target_185258831
Those hashes doesn't ring a bell, but when I checked
那些散列并没有敲响警钟,但是当我检查
c:\ProgramData\Oracle\Java\javapath_target_181743567>.\java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
c:\ProgramData\Oracle\Java\javapath_target_185258831>.\java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
so to make Java 8 default again I had to delete the link as described here
所以要再次使 Java 8 成为默认值,我必须按照此处所述删除链接
rmdir javapath
and recreate with Java I wanted
并用我想要的 Java 重新创建
mklink /D javapath javapath_target_181743567
tested:
测试:
c:\ProgramData\Oracle\Java>java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
** update (Java 10) **
** 更新(Java 10)**
With Java 10 it is similar, only javapath is in c:\Program Files (x86)\Common Files\Oracle\Java\ which is strange as I installed 64-bit IMHO
与 Java 10 类似,只有 javapath 在 c:\Program Files (x86)\Common Files\Oracle\Java\ 中,这很奇怪,因为我安装了 64 位恕我直言
.\java -version
java version "10.0.2" 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)
回答by ndtc
an alterable way to run an .jar app is create an .bat cmd for it. for example, you have jre10 and jre8 installed on your pc,and jre10 is your default jre. but your jar is specified to work with jre8,following cmd will work:
运行 .jar 应用程序的一种可变方式是为其创建一个 .bat cmd。例如,您的电脑上安装了 jre10 和 jre8,而 jre10 是您的默认 jre。但是您的 jar 被指定为与 jre8 一起使用,以下 cmd 将起作用:
"C:\Program Files\Java\jre1.8.0_181\bin\java.exe" -jar JabRef-4.3.1.jar