在 Windows 下同时运行多个 Java 版本

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

Multiple Java versions running concurrently under Windows

java

提问by Daniel Stanca

We have a couple of applications running on Java 5 and would like now to bring in an application based on Java 6. Can both java versions live together under Windows?

我们有几个运行在 Java 5 上的应用程序,现在想引入一个基于 Java 6 的应用程序。两个 Java 版本可以在 Windows 下一起运行吗?

Is there any control panel to set the appropriate Java version for different applications, or any other way to set up, what version of Java will be used to run that particular application?

是否有任何控制面板可以为不同的应用程序设置适当的 Java 版本,或者有任何其他设置方式,将使用哪个版本的 Java 来运行该特定应用程序?

采纳答案by reallyinsane

Of course you can use multiple versions of Java under Windows. And different applications can use different Java versions. How is your application started? Usually you will have a batch file where there is something like

当然,您可以在 Windows 下使用多个版本的 Java。并且不同的应用程序可以使用不同的 Java 版本。您的应用程序是如何启动的?通常你会有一个批处理文件,其中有类似的东西

java ...

This will search the Java executable using the PATH variable. So if Java 5 is first on the PATH, you will have problems running a Java 6 application. You should then modify the batch file to use a certain Java version e.g. by defining a environment variable JAVA6HOMEwith the value C:\java\java6(if Java 6 is installed in this directory) and change the batch file calling

这将使用 PATH 变量搜索 Java 可执行文件。因此,如果 Java 5 位于 PATH 的第一个位置,那么在运行 Java 6 应用程序时就会出现问题。然后,您应该修改批处理文件以使用某个 Java 版本,例如通过定义JAVA6HOME具有值的环境变量C:\java\java6(如果 Java 6 安装在此目录中)并更改批处理文件调用

%JAVA6HOME%\bin\java ...

回答by Moshe

It is absolutely possible to install side-by-side several JRE/JDK versions. Moreover, you don't have to do anything special for that to happen, as Sun is creating a different folder for each (under Program Files).

绝对可以并行安装多个 JRE/JDK 版本。此外,您不必为此做任何特别的事情,因为 Sun 正在为每个文件夹创建一个不同的文件夹(在 Program Files 下)。

There is no control panel to check which JRE works for each application. Basically, the JRE that will work would be the first in your PATH environment variable. You can change that, or the JAVA_HOME variable, or create specific cmd/bat files to launch the applications you desire, each with a different JRE in path.

没有控制面板来检查哪个 JRE 适用于每个应用程序。基本上,将工作的 JRE 将是您的 PATH 环境变量中的第一个。您可以更改它或 JAVA_HOME 变量,或创建特定的 cmd/bat 文件来启动您想要的应用程序,每个文件在路径中都有不同的 JRE。

回答by Ruben

It should be possible changing setting the JAVA_HOME environment variable differently for specific applications.

应该可以针对特定应用程序以不同方式更改 JAVA_HOME 环境变量的设置。

When starting from the command line or from a batch script you can use set JAVA_HOME=C:\...\j2dskXXXto change the JAVA_HOME environment.

从命令行或批处理脚本启动时,您可以set JAVA_HOME=C:\...\j2dskXXX用来更改 JAVA_HOME 环境。

It is possible that you also need to change the PATHenvironment variable to use the correct java binary. To do this you can use set PATH=%JAVA_HOME%\bin;%PATH%.

您可能还需要更改PATH环境变量以使用正确的 java 二进制文件。为此,您可以使用set PATH=%JAVA_HOME%\bin;%PATH%.

回答by jcoder

Invoking Java with "java -version:1.5", etc. should run with the correct version of Java. (Obviously replace 1.5 with the version you want.)

使用“java -version:1.5”等调用 Java 应该使用正确的 Java 版本运行。(显然将 1.5 替换为您想要的版本。)

If Java is properly installed on Windows there are paths to the vm for each version stored in the registry which it uses so you don't need to mess about with environment versions on Windows.

如果 Java 正确安装在 Windows 上,那么它使用的注册表中存储的每个版本都有指向 vm 的路径,因此您无需在 Windows 上弄乱环境版本。

回答by Peter Lawrey

If you use Java Web Start(you can start applications from any URL, even the local file system) it will take care of finding the right version for your application.

如果您使用Java Web Start(您可以从任何 URL 启动应用程序,甚至是本地文件系统),它将负责为您的应用程序找到正确的版本。

回答by Drikus

I was appalled at the clumsiness of the CLASSPATH, JAVA_HOME, and PATH ideas, in Windows, to keep track of Java files. I got here, because of multiple JREs, and how to content with it. Without regurgitating information, from a guy much more clever than me, I would rather point to to his article on this issue, which for me, resolves it perfectly.

我对在 Windows 中跟踪 Java 文件的 CLASSPATH、JAVA_HOME 和 PATH 想法的笨拙感到震惊。我来到这里,因为有多个 JRE,以及如何满足它。没有反刍信息,来自一个比我更聪明的人,我宁愿指向他关于这个问题的文章,对我来说,它完美地解决了这个问题。

Article by: Ted Neward: Multiple Java Homes: Giving Java Apps Their Own JRE

文章作者:Ted Neward:多个 Java 主页:为 Java 应用程序提供自己的 JRE

With the exponential growth of Java as a server-side development language has come an equivablent exponential growth in Java development tools, environments, frameworks, and extensions. Unfortunately, not all of these tools play nicely together under the same Java VM installation. Some require a Servlet 2.1-compliant environment, some require 2.2. Some only run under JDK 1.2 or above, some under JDK 1.1 (and no higher). Some require the "com.sun.swing" packages from pre-Swing 1.0 days, others require the "javax.swing" package names.

Worse yet, this problem can be found even within the corporate enterprise, as systems developed using Java from just six months ago may suddenly "not work" due to the installation of some Java Extension required by a new (seemingly unrelated) application release. This can complicate deployment of Java applications across the corporation, and lead customers to wonder precisely why, five years after the start of the infamous "Installing-this-app-breaks-my-system" woes began with Microsoft's DLL schemes, we still haven't progressed much beyond that. (In fact, the new .NET initiative actually seeks to solve the infamous "DLL-Hell" problem just described.)

This paper describes how to configure a Java installation such that a given application receives its own, private, JRE, allowing multiple Java environments to coexist without driving customers (or system administrators) insane...

随着 Java 作为服务器端开发语言的指数级增长,Java 开发工具、环境、框架和扩展也同样呈指数级增长。不幸的是,并非所有这些工具都可以在同一个 Java VM 安装下很好地协同工作。有些需要符合 Servlet 2.1 的环境,有些需要 2.2。一些仅在 JDK 1.2 或更高版本下运行,一些在 JDK 1.1 下(并且没有更高版本)。有些需要 Swing 1.0 之前的“com.sun.swing”包,其他需要“javax.swing”包名称。

更糟糕的是,这个问题甚至可以在企业内部发现,因为仅仅在六个月前使用 Java 开发的系统可能会突然“无法工作”,因为安装了新的(看似无关的)应用程序版本所需的一些 Java 扩展。这可能会使整个公司的 Java 应用程序部署复杂化,并让客户想知道为什么在臭名昭著的“安装此应用程序破坏我的系统”问题始于 Microsoft 的 DLL 方案五年后,我们仍然没有并没有取得太大进展。(事实上​​,新的 .NET 计划实际上试图解决刚才描述的臭名昭著的“DLL-Hell”问题。)

本文描述了如何配置 Java 安装,以便给定的应用程序接收自己的私有 JRE,从而允许多个 Java 环境共存,而不会让客户(或系统管理员)发疯……

回答by jan.supol

Or use links. While it is rather unpleasant to update the PATH in a running environment, it's easy to recreate a link to a new version of JRE/JDK. So:

或者使用链接。虽然在运行环境中更新 PATH 相当不愉快,但很容易重新创建指向新版本 JRE/JDK 的链接。所以:

  • install different versions of JDK you want to use
  • create a link to that folder either by junctionor by built-in mklink command
  • set the PATH to the link
  • If other version of java is to be used, delete the link, create a new one, PATH/JAVA_HOME/hardcoded scripts remain untouched
  • 安装要使用的不同版本的 JDK
  • 通过联结或内置 mklink 命令创建指向该文件夹的链接
  • 将 PATH 设置为链接
  • 如果要使用其他版本的java,删除链接,新建一个,PATH/JAVA_HOME/hardcoded scripts保持不变

回答by Felipe Ferreira

I use a simple script when starting jmeter with my own java versin

使用我自己的 java 版本启动 jmeter 时,我使用了一个简单的脚本

setlocal set JAVA_HOME="c:\java8" set PATH=%JAVA_HOME%\bin;%PATH%; java -version

setlocal set JAVA_HOME="c:\java8" set PATH=%JAVA_HOME%\bin;%PATH%; java -version

to have a java "portable" you can use this method here:

要拥有 Java“便携式”,您可以在此处使用此方法:

https://www.whitebyte.info/programming/java/how-to-install-a-portable-jdk-in-windows-without-admin-rights

https://www.whitebyte.info/programming/java/how-to-install-a-portable-jdk-in-windows-without-admin-rights

回答by eliana_zulato

Using Java Web Start, you can install multiple JRE, then call what you need. On win, you can make a .bat file:

使用Java Web Start,你可以安装多个JRE,然后调用你需要的。在 win 上,您可以制作一个 .bat 文件:

1- online version: <your_JRE_version\bin\javaws.exe> -localfile -J-Djnlp.application.href=<the url of .jnlp file.jnlp> -localfile -J "<path_temp_jnlp_file_.jnlp>"

1- 在线版本:< your_JRE_version\bin\javaws.exe> -localfile -J-Djnlp.application.href=< .jnlp 文件的url.jnlp> -localfile -J "< path_temp_jnlp_file_.jnlp>"

2- launch from cache: <your_JRE_version\bin\javaws.exe> -localfile -J "<path_of_your_local_jnlp_file.jnlp>"

2- 从缓存启动:< your_JRE_version\bin\javaws.exe> -localfile -J "< path_of_your_local_jnlp_file.jnlp>"

回答by Naresh Joshi

We can install multiple versions of Java Development kits on the same machine using SDKMan.

我们可以使用 SDKMan 在同一台机器上安装多个版本的 Java 开发工具包。

Some points about SDKMan are as following:

SDKMan 的一些要点如下:

  1. SDKMan is free to use and it is developed by the open source community.
  2. SDKMan is written in bashand it only requires curland zip/unzipprograms to be present on your system.
  3. SDKMan can install around 29 Software Development Kits for the JVM such as Java, Groovy, Scala, Kotlin and Ceylon. Ant, Gradle, Grails, Maven, SBT, Spark, Spring Boot, Vert.x.
  4. We do not need to worry about setting the _HOMEand PATHenvironment variables because SDKMan handles it automatically.
  1. SDKMan 是免费使用的,由开源社区开发。
  2. SDKMan 是用bash编写的,它只需要在您的系统上存在curlzip/unzip程序。
  3. SDKMan 可以为 JVM 安装大约 29 个软件开发工具包,例如 Java、Groovy、Scala、Kotlin 和 Ceylon。Ant、Gradle、Grails、Maven、SBT、Spark、Spring Boot、Vert.x。
  4. 我们不需要担心设置_HOMEPATH环境变量,因为 SDKMan 会自动处理它。

SDKMan can run on any UNIX based platforms such as Mac OSX, Linux, Cygwin, Solaris and FreeBSD and we can install it using following commands:

SDKMan 可以在任何基于 UNIX 的平台上运行,例如 Mac OSX、Linux、Cygwin、Solaris 和 FreeBSD,我们可以使用以下命令安装它:

$ curl -s "https://get.sdkman.io" | bash  
$ source "$HOME/.sdkman/bin/sdkman-init.sh" 

Because SDKMan is written in bashand only requires curland zip/unzipto be present on your system. You can install SDKMan on windows as well either by first installing Cygwinor Git Bash for Windowsenvironment and then running above commands.

因为 SDKMan 是用bash编写的,并且只需要curlzip/unzip出现在您的系统上。您也可以通过首先为 Windows环境安装CygwinGit Bash,然后运行上面的命令,在 Windows上安装 SDKMan 。

Command sdk list javawill give us a list of java versions which we can install using SDKMan.

命令sdk list java将为我们提供一个 Java 版本列表,我们可以使用 SDKMan 安装这些版本。

Installing Java 8

安装 Java 8

$ sdk install java 8.0.201-oracle

Installing Java 9

安装 Java 9

$ sdk install java 9.0.4-open 

Installing Java 11

安装 Java 11

$ sdk install java 11.0.2-open

Uninstalling a Java version

卸载 Java 版本

In case you want to uninstall any JDK version e.g., 11.0.2-open you can do that as follows:

如果您想卸载任何 JDK 版本,例如 11.0.2-open,您可以按如下方式进行:

$ sdk uninstall java 11.0.2-open

Switching current Java version

切换当前 Java 版本

If you want to activate one version of JDK for all terminals and applications, you can use the command

如果要为所有终端和应用程序激活一个版本的JDK,可以使用命令

sdk default java <your-java_version>

Above commands will also update the PATH and JAVA_HOME variables automatically. You can read more on my article How to Install Multiple Versions of Java on the Same Machine.

以上命令还将自动更新 PATH 和 JAVA_HOME 变量。您可以在我的文章如何在同一台机器上安装多个版本的 Java 中阅读更多内容。