首页 > 编程语言 > 详细

Java SE 8安装之后Java环境变量变化

时间:2021-04-01 10:21:07      阅读:18      评论:0      收藏:0      [点我收藏+]

Java SE 8安装之后,没有配置JAVA_HOME,CLASSPATH,PATH等系统变量的情况下,打开cmdjava -version竟然有效,输出下java -verbose可以查看详细信息。为了找到从哪里加载的java.exe,我上网进行了一番搜索。终于找到了答案。

JDK installer or JRE installer的行为

System Runtime Configuration File
中的System Runtime Configuration File章节说明了

After installing the JRE, the installer creates the system runtime configuration file.
In JDK 6, after using an installer configuration file to install the JRE, the installer saves it as a system runtime configuration file in %ALLUSERSPROFILE%\Oracle\Java\java_settings.cfg.
In JDK 8 and above, after using an installer configuration file to install the JRE, the installer saves it as a system runtime configuration file in C:\Program Files (x86)\Common Files\Oracle\Java\java_settings.cfg

The value of the environment variable %ALLUSERSPROFILE% is typically C:\ProgramData.

其中最重要的骚操作就是Oracle JDK/JRE把java相关的东西丢到了C:\Program Files (x86)\Common Files下面,而这个文件夹默认是在系统变量PATH中的,这就造成了,你不配置JAVA_HOME等相关变量,照样能够使用Java相关的命令。

注意:JDK/JRE的行为在需要多版本JDK/JRE共存时容易产生问题,所以如果你需要使用指定的JDK/JRE版本时,请手动设置JAVA_HOME或者在启动Java应用的console中提前设置JAVA_HOME变量。

手动设置JAVA_HOME,避免加载默认位置的JDK/JRE

因为Java SE 8添加了java.exe,javaw.exe等路径到系统变量,所以如果希望使用不同的JDK/JRE版本时,需要手动设置JAVA_HOME

因为Windows在查找相关CMD是安装顺序来查找的,所以把JAVA_HOME放在PATH环境变量的第一个,这样就能够使用指定的JDK/JRE版本。如果JAVA_HOME放到C:\Program Files (x86)\Common Files\之后,那么配置的JAVA_HOME就不会生效。

验证Java SE 8配置Java路径到Windows环境变量

Java SE 8 Update 171 Uses Different Windows Path (Doc ID 2412304.1)
Last updated on NOVEMBER 08, 2020

APPLIES TO:
Java SE JDK and JRE - Version 8 and later
Information in this document applies to any platform.
SYMPTOMS
When installing JRE 8 Update 171 for Windows x64, the Java installer (both .exe and Enterprise Installer) prepends the Windows Path environment variable with the 32-bit path:
C:\Program Files (x86)\Common Files\Oracle\Java\javapath

Previous 64-bit Java 8 releases (8 Update162 and older) prepend the 64-bit path: C:\ProgramData\Oracle\Java\javapath to the Windows Path environment variable.

The issue can be reproduced with the following steps:

  1. Remove all Java installations

  2. Clean up any lingering register keys with regedit

  3. Clean up any lingering file/folder structures

  4. Manually remove any javapath from Windows Path variable, if any exists after the uninstallation of Java

      a. Open the System Control Panel
    
      b. Click Advanced System Settings
    
      c. Click Environement Variables
    
      d. Under System Variables, double-click the Path variable
    
      e. Remove any javapath path if present
    
  5. Restart system

  6. Use regedit to verify no Java registry keys or file artifacts remain

  7. Verify Windows PATH does not contain any javapath

  8. Install JRE 8u162

  9. Verify the System Path variable is now prepended with 64-bit path: C:\ProgramData\Oracle\Java\javapath;

  10. Remove JRE 8u162

  11. Verify Windows PATH does not contain any javapath

  12. Install JRE 8u171

  13. Verify the System Path variable is prepended with 32-bit path: C:\Program Files (x86)\Common Files\Oracle\Java\javapath;

参考

Java SE 8 Update 171 Uses Different Windows Path

use-installer-configuration-file-install-jre

which-jre-does-c-programdata-oracle-java-javapath-java-exe-use

how-to-set-custom-java-path-after-installing-jdk-8

Java SE 8安装之后Java环境变量变化

原文:https://www.cnblogs.com/betgar/p/14604677.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!