如何设置默认 Java 安装/运行时 (Windows)?

发布于 2024-07-13 07:24:34 字数 325 浏览 5 评论 0 原文

我的情况是,我已经安装了JDK,但无法在浏览器中运行小程序(我可能没有安装JRE)。

但是,当我安装 JRE 时,它会破坏我的 JDK 作为默认运行时的能力。 这几乎破坏了一切(Eclipse、Ant)——因为它们需要服务器 JVM。

现在没有 JAVA_HOME 环境变量 - 它似乎只是使用了一些注册表魔法(设置系统路径也没有用)。 以前,我在使用 JRE 恢复 JDK 后刚刚将其卸载。 这次我想好好修复一下。

jre 自动更新程序也体现了这一点 - 曾几何时,我使用 JDK 和 JRE 进行了工作设置,但它更新并破坏了所有内容。

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

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.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

来日方长 2024-07-20 07:24:34

这在 Windows 上有点痛苦。 这就是我所做的。

c:\install\jdk\sun\6u11路径中安装最新的Sun JDK,例如6u11,然后让安装程序在默认位置安装公共JRE( >c:\program files\blah)。 这将为大多数事情设置默认的 JRE。

根据需要安装较旧的 JDK,例如 c:\install\jdk\sun\5u18 中的 5u18,但不要安装公共 JRE。

在开发过程中,我有一个小批处理文件,用于为每个 JDK 版本设置命令提示符。 本质上只需设置JAVA_HOME=c:\jdk\sun\JDK_DESIRED,然后设置PATH=%JAVA_HOME%\bin;%PATH%。 这会将所需的 JDK 放在路径的首位,任何辅助工具(例如 Ant 或 Maven)都可以使用 JAVA_HOME 变量。

该路径很重要,因为大多数公共 JRE 安装都会将链接的可执行文件放在 c:\WINDOWS\System32\java.exe 中,这通常覆盖大多数其他设置。

This is a bit of a pain on Windows. Here's what I do.

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.

Install older JDKs as necessary, like 5u18 in c:\install\jdk\sun\5u18, but don't install the public JREs.

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.

The path is important because most public JRE installs put a linked executable at c:\WINDOWS\System32\java.exe, which usually overrides most other settings.

暮年 2024-07-20 07:24:34

我已将属性对话框中的 Eclipse 启动快捷方式的行为

"E:\Program Files\eclipse\eclipse.exe"

修补为

"E:\Program Files\eclipse\eclipse.exe" -vm "E:\Program Files\Java\jdk1.6.0_30\bin"

在 Eclipse 文档中

它只是一个补丁,因为它取决于修复问题的快捷方式...

替代方法是设置参数永久存在于 eclipse 初始化文件中。

I have patched the behaviour of my eclipse startup shortcut in the properties dialogue

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.

梦纸 2024-07-20 07:24:34

我刚刚遇到了这个问题(Windows 7 上的 Java 1.8 与 Java 9),我的发现是:

短版本

默认似乎是(因为路径条目)

c:\ProgramData\Oracle\Java\javapath\java -version

选择您想要的版本(测试,使用选项卡在 cmd 中完成,不确定这些数字代表什么),我有 2 个选项,请参阅更长的版本以了解详细信息,

c:\ProgramData\Oracle\Java\javapath_target_[tab]

删除连接/链接并链接到您的版本(在我的情况下,Java 8 以 181743567 结尾)

rmdir javapath
mklink /D javapath javapath_target_181743567

更长的版本:

Java 9 不起作用后重新安装 Java 1.8。 安装顺序是jdk1.8.0_74,jdk-9.0.4并尝试使Java 8默认为jdk1.8.0_162...

安装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)

我在路径中看到的是

Path=...;C:\ProgramData\Oracle\Java\javapath;...

所以我检查了什么我发现它是一个连接点(链接),

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

这些哈希值并没有敲响警钟,但是当我检查

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)

以便再次使 Java 8 默认时,我必须按照所述删除链接 此处

rmdir javapath

并使用我想要测试的 Java 重新创建

mklink /D javapath javapath_target_181743567

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)

** 更新 (Java 10) **

使用 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)

I just had that problem (Java 1.8 vs. Java 9 on Windows 7) and my findings are:

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

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)

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

After jdk1.8.0_162 installation I still have

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

rmdir javapath

and recreate with Java I wanted

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

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 -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)
苏别ゝ 2024-07-20 07:24:34

经过多次尝试,我发现路口的做法更方便。 这与 Linux 中解决这个问题的方式非常相似。

基本上,它包括在 c:\tools\java\default 和您想要在系统中默认使用的 java 的实际版本之间建立一个链接


##如何设置:

  1. 下载junction并确保将它在你的路径中
    环境变量
  2. 这样设置你的环境:
    - PATH 指向 ONLY 此 jre c:\tools\java\default\bin
    - JAVA_HOME 指向 `c:\tools\java\default
  3. 将所有 jre-s 存储在一个文件夹中(如果您在 Program FILEs 文件夹中这样做,您可能会遇到一些
    • C:\tools\Java\JRE_1.6
    • C:\tools\Java\JRE_1.7
    • C:\tools\Java\JRE_1.8
  4. 打开命令提示符并 cd 到 C:\tools\Java\
  5. 执行 junction default JRE_1.6

这将创建一个 连接(或多或少类似于 Linux 中的符号链接)C:\tools\java\ defaultC:\tools\java\JRE_1.6

这样,您的默认 java 就始终位于 c:\tools\java\default 中。

如果您需要将默认 java 更改为 1.8 版本,则只需执行

junction -d default
junction default JRE_1.8 

然后您可以使用批处理文件来执行此操作,而无需命令提示符,例如
set_jdk8.bat
set_jdk7.bat

根据 @С֏аџdеМаятіи

编辑的建议:从 Windows 版本 > XP 开始,您应该使用 mklink /J default JRE_1.8

After many attempts, I found the junction approach more convenient. This is very similar on how this problem is solved in linux.

Basically it consists of having a link between c:\tools\java\default and the actual version of java you want to use as default in your system.


##How to set it:

  1. Download junction and make sure to put it in your PATH
    environment variable
  2. Set your environment this way:
    - PATH pointing to ONLY to this jre c:\tools\java\default\bin
    - JAVA_HOME pointing to `c:\tools\java\default
  3. 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
  4. Open a command prompt and cd to C:\tools\Java\
  5. Execute 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

In this way you will always have your default java in c:\tools\java\default.

If you then need to change your default java to the 1.8 version you just need to execute

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

As suggested from @СӏаџԁеМаятіи

EDIT: From windows versions >XP , you should use mklink /J default JRE_1.8

彩虹直至黑白 2024-07-20 07:24:34

我只需安装我需要的所有 JDK 版本,最新安装的版本将成为默认版本,因此我只需在必要时重新安装我想要默认的版本。

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.

格子衫的從容 2024-07-20 07:24:34

我在 C:\Java 中安装了多个 JDK(1.4、1.5、1.6)及其 JRE。 然后我让 Sun 更新 C:\Program Files\Java 中的公共 JRE。
最近有一个改进,安装在jre6中。 以前,每个新版本都有一个不同的文件夹(1.5.0_4、1.5.0_5 等),这会占用大量空间

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

愁杀 2024-07-20 07:24:34

需要从环境中删除 C:\Program Files (x86)\Common Files\Oracle\Java\javapath 并替换为 JAVA_HOME,这对我来说效果很好

Need to remove C:\Program Files (x86)\Common Files\Oracle\Java\javapath from environment and replace by JAVA_HOME which is works fine for me

尴尬癌患者 2024-07-20 07:24:34

运行 .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

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:

"C:\Program Files\Java\jre1.8.0_181\bin\java.exe" -jar JabRef-4.3.1.jar
提笔落墨 2024-07-20 07:24:34

这个问题已被解决,并已在 2020Windows 10 中得到解决。 我使用的是Java 8 RE14.1 JDK,它运行良好,直到Eclipse升级到版本2020-09 。 之后,我无法运行 Eclipse,因为它需要使用 Java 11 或更高版本,并且仅找到 8 版本。 这是因为“Path”环境变量的顺序:

环境变量设置

我想C:\Program Files (x86)\Common Files\Oracle\Java\javapath< /code> 是链接到已安装的 JRE exe 文件的路径(在我的例子中是 Java 8),通过在 %JAVA_HOME% 之后向下移动此链接解决了问题,导致到 Java 14.1/bin 文件夹。

似乎环境变量的顺序会影响请求可执行文件时搜索文件夹的顺序。
感谢您的评论或更好的解释。

Stacked by this issue and have resolved it in 2020, in Windows 10. I'm using Java 8 RE and 14.1 JDK and it worked well until Eclipse upgrade to version 2020-09. After that I can't run Eclipse because it needed to use Java 11 or newer and it found only 8 version. It was because of order of environment variables of "Path":

environment variables setting

I suppose C:\Program Files (x86)\Common Files\Oracle\Java\javapath is path to link to installed JRE exe files (in my case Java 8) and the issue was resolved by move down this link after %JAVA_HOME%, what leads to Java 14.1/bin folder.

EV setting after edit

It seems that order of environment variables affects order of searched folders while executable file is requested.
Thanks for your comment or better explanation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文