如何在 Windows (7) 上更改 Java 运行时版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3333553/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:19:01  来源:igfitidea点击:

How can I change the Java Runtime Version on Windows (7)?

javawindowsregistrycontrolspanel

提问by tomkpunkt

How can I change the Java Runtime Version on Windows.

如何在 Windows 上更改 Java 运行时版本。

I installed Java 7 for some tests, and now I need the old java6 as system default, but I don't want to uninstall the Java 7 (I need it for later tests). Can I change the system-used JREin the control panel/Java/JRE tab? I can change/edit/add/delete the user-used version, but not the system-used.

我为一些测试安装了 Java 7,现在我需要旧的 java6 作为系统默认值,但我不想卸载 Java 7(我需要它用于以后的测试)。我可以在控制面板/Java/JRE 选项卡中更改系统使用的JRE吗?我可以更改/编辑/添加/删除用户使用的版本,但不能更改系统使用的版本。

采纳答案by Carl Smotricz

For Java applications, i.e. programs that are delivered (usually) as .jarfiles and started with java -jar xxx.jaror via a shortcut that does the same, the JRE that will be launched will be the first one found on the PATH.

对于 Java应用程序,即(通常)作为.jar文件交付并使用java -jar xxx.jar或通过执行相同操作的快捷方式启动的程序,将启动的 JRE 将是PATH.

If you installed a JRE or JDK, the likely places to find the .exes are below directories like C:\Program Files\JavaSoft\JRE\x.y.z. However, I've found some "out of the box" Windows installations to (also?) have copies of java.exeand javaw.exein C:\winnt\system32(NT and 2000) or C:\windows\system(Windows 95, 98). This is usually a pretty elderly version of Java: 1.3, maybe? You'll want to do java -versionin a command window to check that you're not running some antiquated version of Java.

如果您安装了 JRE 或 JDK,则可能可以找到.exes 的位置在 C:\Program Files\JavaSoft\JRE\x.y.z. 但是,我发现一些“开箱即用”的 Windows 安装(也?)在(NT 和 2000)或(Windows 95、98 )中有java.exe和 的副本。这通常是 Java 的一个相当老的版本:1.3,也许吧?您需要在命令窗口中检查您没有运行某些过时的 Java 版本。javaw.exeC:\winnt\system32C:\windows\systemjava -version

You can of course override the PATH setting or even do without it by explicitly stating the path to java.exe / javaw.exe in your command line or shortcut definition.

您当然可以通过在命令行或快捷方式定义中明确声明 java.exe / javaw.exe 的路径来覆盖 PATH 设置,甚至可以不使用它。



If you're running appletsfrom the browser, or possibly also Java Web Start applications(they look like applications insofar as they have their own window, but you start them from the browser), the choice of JRE is determined by a set of registry settings:

如果您从浏览器运行小程序,或者也可能是Java Web Start 应用程序(它们看起来像应用程序,因为它们有自己的窗口,但您从浏览器启动它们),JRE 的选择由一组注册表决定设置:

Key: HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment
Name: CurrentVersion
Value: (e.g.) 1.3

More registry keys are created using this scheme:

使用此方案创建更多注册表项:

(e.g.) 
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment.3   
HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment.3.1

i.e. one for the major and one including the minor version number. Each of these keys has values like these (examples shown):

即一个用于主要版本号,一个包括次要版本号。这些键中的每一个都有这样的值(显示的示例):

JavaHome    : C:\program Files\JavaSoft\JRE.3.1
RuntimeLib  : C:\Program Files\JavaSoft\JRE.3.1\bin\hotspot\jvm.dll
MicroVersion: 1

... and your browser will look to these settings to determine which JRE to fire up.

...您的浏览器将查看这些设置以确定要启动哪个 JRE。

Since Java versions are changing pretty frequently, there's now a "wizard" called the "Java Control Panel" for manually switching your browser's Java version. This works for IE, Firefox and probably others like Opera and Chrome as well: It's the 'Java' applet in Windows' System Settingsapp. You get to pick any one of the installed JREs. I believe that wizard fiddles with those registry entries.

由于 Java 版本变化非常频繁,现在有一个名为“Java 控制面板”的“向导”,用于手动切换浏览器的 Java 版本。这适用于 IE、Firefox 以及其他可能的 Opera 和 Chrome:它是 WindowsSystem Settings应用程序中的“Java”小程序。您可以选择任何一种已安装的 JRE。我相信向导会摆弄这些注册表项。

If you're like me and have "uninstalled" old Java versions by simply wiping out directories, you'll find these "ghosts" among the choices too; so make sure the JRE you choose corresponds to an intact Java installation!

如果您像我一样通过简单地清除目录来“卸载”旧的 Java 版本,那么您也会在选择中发现这些“幽灵”;所以请确保您选择的 JRE 对应于完整的 Java 安装!



Some other answers are recommending setting the environmentvariable JAVA_HOME. This is meanwhile outdated advice. Sun came to realize, around Java 2, that this environment setting is

其他一些答案建议设置环境变量JAVA_HOME。这是同时过时的建议。Sun 开始意识到,围绕 Java 2,这个环境设置是

  1. unreliable, as users often set it incorrectly, and
  2. unnecessary, as it's easy enough for the runtime to find the Java library directories, knowing they're in a fixed path relative to the path from which java.exe or javaw.exe was launched.
  1. 不可靠,因为用户经常错误地设置它,并且
  2. 不必要,因为运行时很容易找到 Java 库目录,知道它们位于相对于启动 java.exe 或 javaw.exe 的路径的固定路径中。

There's hardly any modern Java software left that needs or respects the JAVA_HOMEenvironment variable.

几乎没有任何现代 Java 软件需要或尊重JAVA_HOME环境变量。



More Information:

更多信息:

...and some useful information on multi-version support:

...以及一些关于多版本支持的有用信息:

回答by Syntax

Update your environment variables

更新您的环境变量

Ensure the reference to java/bin is up to date in 'Path'; This may be automatic if you have JAVA_HOME or equivalent set. If JAVA_HOME is set, simply update it to refer to the older JRE installation.

确保在“路径”中对 java/bin 的引用是最新的;如果您有 JAVA_HOME 或等效设置,这可能是自动的。如果设置了 JAVA_HOME,只需更新它以引用旧的 JRE 安装。

回答by David Young

All you need to do is set the PATH environment variable in Windows to point to where your java6 bin directory is instead of the java7 directory.

您需要做的就是在 Windows 中设置 PATH 环境变量以指向 java6 bin 目录而不是 java7 目录的位置。

Right click My Computer > Advanced System Settings > Advanced > Environmental Variables

右键单击“我的电脑”>“高级系统设置”>“高级”>“环境变量”

If there is a JAVA_HOME environment variable set this to point to the correct directory as well.

如果有 JAVA_HOME 环境变量,请将其设置为指向正确的目录。

回答by Jules

Since Java 1.6, a java.exe is installed into %windir%\system32 that supports a "-version" command line option. You can use this to select a specific version to run, e.g.:

从 Java 1.6 开始,java.exe 安装到 %windir%\system32 中,支持“-version”命令行选项。您可以使用它来选择要运行的特定版本,例如:

java -version:1.7 -jar [path to jar file]

will run a jar application in java 1.7, if it is installed.

如果已安装,将在 java 1.7 中运行 jar 应用程序。

See Oracle's documentation here: http://docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html

请参阅此处的 Oracle 文档:http: //docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html

回答by Shazia Tabassum

Go to control panel --> Java You can select the active version here

转到控制面板 --> Java 您可以在此处选择活动版本

回答by Asanka Siriwardena

Once I updated my Java version to 8 as suggested by browser. However I had selected to uninstall previous Java 6 version I have been used for coding my projects. When I enter the command in "java -version" in cmdit showed 1.8 and I could not start eclipse IDE run on Java 1.6.

一旦我按照浏览器的建议将我的 Java 版本更新为 8。但是,我选择卸载以前用于编码项目的 Java 6 版本。当我在cmd 的“java -version”中输入命令时, 它显示为 1.8 并且我无法启动在 Java 1.6 上运行的 eclipse IDE。

When I installed Java 8 update for the browser it had changed the "PATH" System variable appending "C:\ProgramData\Oracle\Java\javapath" to the beginning. Newly added path pointed to Java vesion 8. So I removed that path from "PATH" System variable and everything worked fine. :)

当我为浏览器安装 Java 8 更新时,它更改了“PATH”系统变量,将“C:\ProgramData\Oracle\Java\javapath”附加到开头。新添加的路径指向 Java 版本 8。所以我从“PATH”系统变量中删除了该路径,一切正常。:)

回答by Mia

I use to work on UNIX-like machines, but recently I have had to do some work with Java on a Windows 7 machine. I have had that problem and this is the I've solved it. It has worked right for me so I hope it can be used for whoever who may have this problem in the future.

我曾经在类 UNIX 机器上工作,但最近我不得不在 Windows 7 机器上使用 Java 做一些工作。我遇到了这个问题,这是我已经解决的。它对我有用,所以我希望它可以用于将来可能遇到此问题的任何人。

These steps are exposed considering a default Java installation on drive C. You should change what it is necessary in case your installation is not a default one.

考虑到驱动器 C 上的默认 Java 安装,这些步骤已公开。如果您的安装不是默认安装,您应该更改必要的内容。

Change Java default VM on Windows 7

在 Windows 7 上更改 Java 默认 VM

Suppose we have installed Java 8 but for whatever reason we want to keep with Java 7.

假设我们已经安装了 Java 8,但出于某种原因我们想继续使用 Java 7。

1- Start a cmd as administrator

1-以管理员身份启动cmd

2- Go to C:\ProgramData\Oracle\Java

2-去 C:\ProgramData\Oracle\Java

3- Rename the current directory javapathto javapath_<version_it_refers_to>. E.g.: rename javapath javapath_1.8

3- 将当前目录javapath重命名为javapath_<version_it_refers_to>。例如:rename javapath javapath_1.8

4- Create a javapath_<version_you_want_by_default>directory. E.g.: mkdir javapath_1.7

4- 创建一个javapath_<version_you_want_by_default>目录。例如:mkdir javapath_1.7

5- cd into it and create the following links:

5- cd 进入其中并创建以下链接:

cd javapath_1.7
mklink java.exe "C:\Program Files\Java\jre7\bin\java.exe"
mklink javaw.exe "C:\Program Files\Java\jre7\bin\javaw.exe"
mklink javaws.exe "C:\Program Files\Java\jre7\bin\javaws.exe"

6- cd out and create a directory link javapathpointing to the desired javapath. E.g.: mklink /D javapath javapath_1.7

6-光盘拿出来,并创建一个目录链接javapath指向期望javapath。例如:mklink /D javapath javapath_1.7

7- Open the register and change the key HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\CurrentVersionto have the value 1.7

7- 打开寄存器并将密钥HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\CurrentVersion更改为值为 1.7

At this point if you execute java -versionyou should see that you are using java version 1.7:

此时,如果您执行,java -version您应该看到您使用的是 Java 1.7 版:

java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

8- Finally it is a good idea to create the environment variable JAVA_HOME. To do that I create a directory link named CurrentVersion in C:\Program Files\Java pointing to the Java version I'm interested in. E.g.:

8- 最后,创建环境变量 JAVA_HOME 是个好主意。为此,我在 C:\Program Files\Java 中创建了一个名为 CurrentVersion 的目录链接,指向我感兴趣的 Java 版本。例如:

cd C:\Program Files\Java\
mklink /D CurrentVersion .\jdk1.7.0_71

9- And once this is done:

9- 一旦完成:

  • Right click My Computer and select Properties.
  • On the Advanced tab, select Environment Variables, and then edit/create JAVA_HOME to point to where the JDK software is located, in that case, C:\Program Files\Java\CurrentVersion
  • 右键单击我的电脑并选择属性。
  • 在 Advanced 选项卡上,选择 Environment Variables,然后编辑/创建 JAVA_HOME 以指向 JDK 软件所在的位置,在这种情况下,为 C:\Program Files\Java\CurrentVersion

回答by Harvey Lin

If you are using windows 10 or windows server 2012, the steps to change the java runtime version is this:

如果您使用的是 windows 10 或 windows server 2012,更改 java 运行时版本的步骤如下:

  1. Open regedit using 'Run'
  2. Navigate to HKEY_LOCAL_MACHINE -> SOFTWARE -> JavaSoft -> Java Runtime Environment
  3. Here you will see all the versions of java you installed on your PC. For me I have several versions of java 1.8 installed, so the folder displayed here are 1.8, 1.8.0_162 and 1.8.0_171
  4. Click the '1.8' folder, then double click the JavaHome and RuntimeLib keys, Change the version number inside to whichever Java version you want your PC to run on. For example, if the Value data of the key is 'C:\Program Files\Java\jre1.8.0_171', you can change this to 'C:\Program Files\Java\jre1.8.0_162'.
  5. You can then verify the version change by typing 'java -version' on the command line.
  1. 使用“运行”打开 regedit
  2. 导航到 HKEY_LOCAL_MACHINE -> SOFTWARE -> JavaSoft -> Java Runtime Environment
  3. 在这里,您将看到您在 PC 上安装的所有 Java 版本。对我来说,我安装了多个版本的 java 1.8,所以这里显示的文件夹是 1.8、1.8.0_162 和 1.8.0_171
  4. 单击“1.8”文件夹,然后双击 JavaHome 和 RuntimeLib 键,将里面的版本号更改为您希望 PC 运行的任何 Java 版本。例如,如果键的值数据为'C:\Program Files\Java\jre1.8.0_171',则可以将其更改为'C:\Program Files\Java\jre1.8.0_162'。
  5. 然后,您可以通过在命令行上键入“java -version”来验证版本更改。