简体   繁体   English

java -version 显示的版本与路径中的不同

[英]java -version showing different version than in path

I have jdk12 and jdk8 on my computer installed.我的电脑上安装了jdk12jdk8 Usually jdk12 is set in the path.通常在路径中设置jdk12 To run some java programs I had set path to jdk8 .要运行一些 java 程序,我已将路径设置为jdk8 Now after changing the path back to /jdk-12.0.1/bin , running java -version on cmd still returns现在将路径改回/jdk-12.0.1/bin后,在 cmd 上运行 java -version 仍然返回

java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)

I decided to assemble my comments to an answer:我决定将我的评论汇总为一个答案:

When calling java on the command line, the OS only uses the PATH environment variable to look for the command.在命令行调用java时,操作系统只使用PATH环境变量来查找命令。 The variable JAVA_HOME is not used here.这里没有使用变量JAVA_HOME

So you simply should examine the PATH variable: The directory C:\Program Files (x86)\Common Files\Oracle\Java\javapath is added to the PATH by the JDK installer.因此,您只需检查PATH变量:JDK 安装程序将目录C:\Program Files (x86)\Common Files\Oracle\Java\javapath添加到PATH中。 I would suggest to remove it, so you have better control what directories are part of the PATH .我建议删除它,这样您就可以更好地控制哪些目录是PATH的一部分。

If you have multiple Java installed, create the JAVA_HOME variable and give the JAVA path which you want to use.如果您安装了多个 Java,请创建JAVA_HOME变量并提供您要使用的 JAVA 路径。 Use only this JAVA_HOME variable in the PATH variable.仅在PATH变量中使用此JAVA_HOME变量。

If you wish to change the default Java, change only the JAVA_HOME variable pointing to the required JAVA.如果您希望更改默认 Java,只需更改指向所需 JAVA 的JAVA_HOME变量。

This way, only one JAVA (which is required) will be present in PATH .这样, PATH中只会出现一个 JAVA (这是必需的)。 Remove all other JAVA references in the PATH variable.删除PATH变量中的所有其他 JAVA 引用。

Also, whenever you change the JAVA_HOME value, open a new terminal (command prompt) to verify this change.此外,每当您更改JAVA_HOME值时,请打开一个新终端(命令提示符)以验证此更改。

Note: If the path contains spaces, use the shortened path name.注意:如果路径包含空格,请使用缩短的路径名。 For example, C:\Progra~1\Java\jdk1.8.0_65

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

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