We need to make a distinction between the two environment variables that are discussed here interchangeably. One is the JAVA_HOME variable. The other is the Path variable. Any process that references the JAVA_HOME variable is looking for the search path to the JDK, not the JRE. The use of JAVA_HOME variable is not meant for the Java compiler itself. The compiler is aware of its own location. The variable is meant for other software to more easily locate the compiler. This variable is typically used by IDE software in order to compile and build applications from Java source code. By contrast, the Windows CMD interpreter, and many other first and third party software references the Path variable, not the JAVA_HOME variable.
用例1:从CMD编译
例如,如果你不使用任何IDE软件,你只是想能够从CMD编译,独立于你当前的工作目录,那么你想要的是正确地设置Path变量。在本例中,您甚至不需要JAVA_HOME变量。因为CMD使用Path,而不是JAVA_HOME来定位Java编译器。
用例2:从IDE编译
但是,如果您正在使用一些IDE软件,那么您必须首先查看文档。它可能需要设置JAVA_HOME,但也可能出于同样的目的使用另一个变量名。多年来,事实上的标准一直是JAVA_HOME,但情况并非总是如此。
用例3:从IDE和CMD编译
如果除了IDE软件之外,您还希望能够从CMD编译,独立于当前工作目录,那么除了JAVA_HOME变量之外,您可能还需要将JDK搜索路径附加到path变量。
JAVA_HOME vs. Path
如果您的问题与编译Java有关,那么您需要检查JAVA_HOME变量和Path(如果适用)。如果您的问题与运行Java应用程序有关,那么您需要检查Path变量。
Path variable is used universally across all operating systems. Because it is defined by the system, and because it's the default variable that's used for locating the JRE, there is almost never any problem running Java applications. Especially not on Windows where the software installers usually set everything up for you. But if you are installing manually, the safest thing to do is perhaps to skip the JAVA_HOME variable altogether and just use the Path variable for everything, for both JDK and the JRE. Any recent version of an IDE software should be able to pick that up and use it.
符号链接
符号链接可以提供另一种引用JDK搜索路径的方法,即通过装载现有的环境变量之一。
我不确定以前版本的Oracle/Sun JDK/JRE版本,但至少jdk1.8.0_74的安装程序将搜索路径C:\ProgramData\Oracle\Java\javapath附加到path变量,并将其放在字符串值的开头。此目录包含到JRE目录中的java.exe、javaw.exe和javaw.exe的符号链接。
因此,至少对于Java 8 JDK,以及独立的Java 8 JRE,不需要为JRE进行环境变量配置。只要你使用安装包来设置它。不过,Windows安装可能会有不同。注意,Oracle JRE是与JDK捆绑在一起的。
如果您发现您的Java JDK配置使用了错误的编译器版本,或者它看起来像魔术一样工作,而没有显式地定义(没有施咒),那么您的环境变量中可能有一个符号链接。你可能需要检查符号链接。