简体   繁体   English

Windows 上的 JAVA_HOME 似乎没有效果

[英]JAVA_HOME on Windows seems to have no effect

I have three different JDKs on my system so I can appropriately switch between projects.我的系统上有三个不同的 JDK,所以我可以在项目之间适当地切换。 Importing the appropriate JDK in my IDE is no issue, but the command line is.在我的 IDE 中导入适当的 JDK 没有问题,但命令行是。 See, despite the fact that JAVA_HOME for both my user as well as the system point to jre1.8.0_261 :看,尽管我的用户和系统的JAVA_HOME都指向jre1.8.0_261

我的系统变量

java --version seems to still think that my default is Amazon's Corretto JDK, which I only need for one specific project: java --version似乎仍然认为我的默认是亚马逊的 Corretto JDK,我只需要一个特定的项目:

C:\Users\jasonfil>java --version
openjdk 11.0.8 2020-07-14 LTS
OpenJDK Runtime Environment Corretto-11.0.8.10.1 (build 11.0.8+10-LTS)

This is also true for Powershell. Powershell 也是如此。 I have also tried in both cmd and Powershell as administrator.我也以管理员身份尝试过cmdPowershell。 Same issue.同样的问题。

Curiously, when I query the environment variable from cmd , I get the intended JDK:奇怪的是,当我从cmd查询环境变量时,我得到了预期的 JDK:

C:\Users\jasonfil>echo %JAVA_HOME%
C:\Program Files (x86)\Java\jre1.8.0_261

While on Powershell, echo just outputs the string %JAVA_HOME literally:在 Powershell 上, echo仅按字面意思输出字符串%JAVA_HOME

PS C:\Users\jasonfil> echo %JAVA_HOME%
%JAVA_HOME%

My goal is to be able to switch between Java versions at will, like the update-alternatives mechanism does in UNIX-based systems.我的目标是能够随意在 Java 版本之间切换,就像基于 UNIX 的系统中的update-alternatives机制一样。 Any ideas?有任何想法吗?

All the outputs you are getting are as expected .您得到的所有输出都符合预期

  1. JAVA_HOME has nothing to do with the Java version. JAVA_HOME与 Java 版本无关。 It is simply a variable that conventionally most of the Java-based software (eg Web & App servers, IDEs etc.) use to refer to find the JDK installation.它只是一个变量,通常大多数基于 Java 的软件(例如 Web & App 服务器、IDE 等)使用它来指代 JDK 安装。 Therefore, you should set it to the base folder of JDK eg C:\Program Files\Java\jdk1.8.0_261 .因此,您应该将其设置为 JDK 的基本文件夹,例如C:\Program Files\Java\jdk1.8.0_261
  2. java -version looks for java.exe which is inside the bin folder of the JDK installation eg C:\Program Files\Java\jdk1.8.0_261\bin . java -version查找位于 JDK 安装的bin文件夹内的java.exe ,例如C:\Program Files\Java\jdk1.8.0_261\bin Therefore, C:\Program Files\Java\jdk1.8.0_261\bin should be one of the values in your PATH variable.因此, C:\Program Files\Java\jdk1.8.0_261\bin应该是PATH变量中的值之一。 If you have already set JAVA_HOME as mentioned above, setting up the path of java.exe becomes easier as you can set %JAVA_HOME%\bin instead of C:\Program Files\Java\jdk1.8.0_261\bin in your PATH variable.如果您已经如上所述设置了JAVA_HOME ,则设置java.exe的路径会变得更容易,因为您可以在PATH变量中设置%JAVA_HOME%\bin而不是C:\Program Files\Java\jdk1.8.0_261\bin
  3. echo %JAVA_HOME% will return you the value set in your JAVA_HOME variable. echo %JAVA_HOME%将返回您在JAVA_HOME变量中设置的值。 Similarly, if you use echo %PATH% , you will get the values set in your PATH variable.同样,如果您使用echo %PATH% ,您将获得在PATH变量中设置的值。

Note: You are not provided with a JRE starting with Java-11.注意:不向您提供从 Java-11 开始的 JRE。

Java doesn't use the JAVA_HOME environment variable. Java 不使用JAVA_HOME环境变量。 Some tools do, especially during installation of the tools, but that's different.有些工具可以,尤其是在安装工具期间,但那是不同的。

Java doesn't even need to be on the PATH , but it is certainly more convenient if it is. Java 甚至不需要在PATH上,但如果是的话肯定会更方便。

The question shows that there is a Java installed at C:\Program Files (x86)\Java\jre1.8.0_261 .问题表明在C:\Program Files (x86)\Java\jre1.8.0_261安装了一个 Java。

It also shows that there is an OpenJDK 11.0.8 somewhere, but doesn't show where, so for this answer, we'll pretend it was installed/unzipped to C:\foo\openjdk-11.0.8 .它还显示某处有一个 OpenJDK 11.0.8,但没有显示在哪里,所以对于这个答案,我们假设它已安装/解压缩到C:\foo\openjdk-11.0.8

The java executable is in the bin folder, so to show that the PATH and JAVA_HOME doesn't matter, run the java command fully qualified. java可执行文件位于bin文件夹中,因此要显示PATHJAVA_HOME无关紧要,请运行完全限定的java命令。 On my machine, I get the following outputs (with different actual paths and I have different versions, but whatever) :在我的机器上,我得到以下输出(具有不同的实际路径,我有不同的版本,但无论如何)

C:\>"C:\Program Files (x86)\Java\jre1.8.0_261\bin\java" -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
C:\>C:\foo\openjdk-11.0.8\bin\java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

Running java unqualified is of course easier, so add the bin folder of Java you want to the front of the PATH .运行不合格的java当然更容易,所以把你想要的java的bin文件夹加到PATH前面 You can even do that in a command prompt to change the "default" Java for that command prompt without affecting your "global default" as specified in that environment property dialog.您甚至可以在命令提示符中更改该命令提示符的“默认”Java,而不会影响该环境属性对话框中指定的“全局默认值”。

You can keep doing it in the same command prompt, if needed, to flip back and forth, but you'd want to limit that, since the PATH will get longer and longer, with lots of redundant entries, the more you do it.如果需要,您可以在同一命令提示符下继续执行此操作来来回切换,但您希望限制这一点,因为PATH会变得越来越长,并且有很多冗余条目,您执行的次数越多。

C:\>set PATH=C:\Program Files (x86)\Java\jre1.8.0_261\bin;%PATH%

C:\>java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

C:\>set PATH=C:\foo\openjdk-11.0.8\bin\java\bin;%PATH%

C:\>java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

Both cmd and Powershell will reference Path variable to check if the requested executable is in the PATH. cmd 和 Powershell 都将引用 Path 变量来检查请求的可执行文件是否在 PATH 中。 The JAVA_HOME variable will only be used by the IDE or the Java applications. JAVA_HOME 变量将仅由 IDE 或 Java 应用程序使用。 So to have the desired JDK in the path, append the JDK path to Path environment variable to either User variables (if you desire this settings should only afffect the current logged in user ie your login) or System Variable.因此,要在路径中包含所需的 JDK,请将路径环境变量的 JDK 路径附加到用户变量(如果您希望此设置只影响当前登录的用户,即您的登录名)或系统变量。

After you add these settings make sure you reopen cmd or Powershell console for the settings to take effect.添加这些设置后,请确保重新打开 cmd 或 Powershell 控制台以使设置生效。

If the PATH environment variable contains an entry for each java installation on your machine, then the console is going to always use the java installation which occurs first in the PATH environment variable.如果 PATH 环境变量包含您机器上每个 java 安装的条目,那么控制台将始终使用首先出现在 PATH 环境变量中的 java 安装。 If you want to use a specific version of java, modify the path environment variable so that version of java's bin folder is declared before any of the other installed versions of java.如果要使用特定版本的 java,请修改路径环境变量,以便在任何其他已安装的 java 版本之前声明 java 的 bin 文件夹版本。

我添加了环境变量,然后重新启动我的机器运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM