JAVA_HOME在Windows上似乎没有任何效果。

huangapple go评论72阅读模式
英文:

JAVA_HOME on Windows seems to have no effect

问题

我在系统上有三个不同版本的JDK,这样我就可以在不同项目之间进行适当的切换。在我的集成开发环境(IDE)中导入适当的JDK没有问题,但在命令行中却有问题。看到,尽管我的用户和系统的JAVA_HOME都指向jre1.8.0_261

java --version 似乎仍然认为我的默认JDK是亚马逊的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)

这对于PowerShell也是正确的。我还尝试了在管理员权限下的cmd和PowerShell中,问题依旧。

奇怪的是,当我从cmd查询环境变量时,我得到了预期的JDK路径:

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

而在PowerShell中,echo 输出了字符串 %JAVA_HOME

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

我的目标是能够随意在不同的Java版本之间切换,就像类UNIX系统中的 update-alternatives 机制那样。有什么想法吗?

英文:

I have three different JDKs on my system so I can appropriately switch between projects. Importing the appropriate JDK in my IDE is no issue, but the command line is. See, despite the fact that JAVA_HOME for both my user as well as the system point to jre1.8.0_261:

JAVA_HOME在Windows上似乎没有任何效果。

java --version seems to still think that my default is Amazon's Corretto JDK, which I only need for one specific project:

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. I have also tried in both cmd and Powershell as administrator. Same issue.

Curiously, when I query the environment variable from cmd, I get the intended 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:

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. Any ideas?

答案1

得分: 3

你所得到的所有输出都与预期相符

  1. JAVA_HOME 与 Java 版本无关。它只是一个变量,传统上大多数基于 Java 的软件(例如 Web 和应用服务器、集成开发环境等)用于指向 JDK 安装的位置。因此,你应该将它设置为 JDK 的基础文件夹,例如 C:\Program Files\Java\jdk1.8.0_261
  2. java -version 寻找的是 JDK 安装目录中 bin 文件夹内的 java.exe 文件,例如 C:\Program Files\Java\jdk1.8.0_261\bin。因此,C:\Program Files\Java\jdk1.8.0_261\bin 应该是你的 PATH 变量中的一个值。如果你已经按照上面提到的方式设置了 JAVA_HOME,那么设置 java.exe 的路径会更容易,因为你可以在 PATH 变量中设置 %JAVA_HOME%\bin,而不是 C:\Program Files\Java\jdk1.8.0_261\bin
  3. echo %JAVA_HOME% 将会返回你在 JAVA_HOME 变量中设置的值。类似地,如果你使用 echo %PATH%,你将会得到在 PATH 变量中设置的值。

注意: 你没有提供 Java 11 及以上版本的 JRE。

英文:

All the outputs you are getting are as expected.

  1. JAVA_HOME has nothing to do with the Java version. It is simply a variable that conventionally most of the Java-based software (e.g. Web & App servers, IDEs etc.) use to refer to find the JDK installation. Therefore, you should set it to the base folder of JDK e.g. 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 e.g. 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. 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.
  3. echo %JAVA_HOME% will return you the value set in your JAVA_HOME variable. Similarly, if you use echo %PATH%, you will get the values set in your PATH variable.

Note: You are not provided with a JRE starting with Java-11.

答案2

得分: 3

Java不使用JAVA_HOME环境变量。一些工具在安装过程中会使用它,尤其是在工具安装期间,但情况是不同的。

Java甚至不需要出现在PATH中,但如果出现在其中,肯定会更加方便。

问题显示在C:\Program Files (x86)\Java\jre1.8.0_261安装了一个Java。

还显示有一个OpenJDK 11.0.8,但没有显示安装在哪里,因此在此答案中,我们假设它被安装或解压缩到了C:\foo\openjdk-11.0.8

java可执行文件位于bin文件夹中,所以为了显示PATHJAVA_HOME无关紧要,可以完全限定地运行java命令。在我的机器上,我得到了以下输出*(实际路径不同,版本也不同,但不影响)*:

C:\>"C:\Program Files (x86)\Java\jre1.8.0_261\bin\java" -version
java版本 "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版本 "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)

当然,运行不带限定符的java更加简便,所以将想要的Java的bin文件夹添加到PATH前面。甚至可以在命令提示符中执行此操作,以更改该命令提示符的“默认”Java,而不影响在环境属性对话框中指定的“全局默认”Java。

如果需要,甚至可以在同一个命令提示符中继续执行此操作,以便在不同的Java之间切换,但应该限制这样做,因为每次执行都会导致PATH变得越来越长,包含越来越多的冗余条目。

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

C:\>java -version
java版本 "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版本 "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)
英文:

Java doesn't use the JAVA_HOME environment variable. 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.

The question shows that there is a Java installed at C:\Program Files (x86)\Java\jre1.8.0_261.

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.

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

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.

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)

答案3

得分: 0

cmd和Powershell都会引用Path变量来检查所请求的可执行文件是否在路径中。JAVA_HOME变量仅会被IDE或Java应用程序使用。因此,为了在路径中使用所需的JDK,请将JDK路径追加到Path环境变量,无论是在用户变量(如果您希望此设置仅影响当前登录的用户,即您的登录)还是系统变量中。

在添加这些设置后,请确保重新打开cmd或Powershell控制台以使设置生效。

英文:

Both cmd and Powershell will reference Path variable to check if the requested executable is in the PATH. The JAVA_HOME variable will only be used by the IDE or the Java applications. 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 i.e. your login) or System Variable.

After you add these settings make sure you reopen cmd or Powershell console for the settings to take effect.

答案4

得分: 0

如果 PATH 环境变量中包含每个 Java 安装在您的计算机上的条目,那么控制台将始终使用在 PATH 环境变量中首先出现的 Java 安装。如果您想使用特定版本的 Java,请修改路径环境变量,使该版本的 Java 的 bin 文件夹在其他已安装的 Java 版本之前声明。

英文:

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

答案5

得分: 0

我添加了环境变量,然后重新启动了我的机器,一切正常运行。

英文:

I added the environment variable and then restarted my machine worked well.

huangapple
  • 本文由 发表于 2020年9月19日 03:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63961748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定