In my java code, I have this line System.getProperty("java.home")
. In some environments, this returns the same value as what has been set JAVA_HOME
as environment variable.
But in some environments, System.getProperty("java.home")
returns completely different value from JAVA_HOME
.
So my question is what's the difference between java.home
and JAVA_HOME
from java perspective?
What i know from my research is JAVA_HOME
is jdk installation path, java.home
is jre installation path, but then why can't it match as jre can be part of jdk installation.
As you stated, JAVA_HOME points to the JDK installation path given by the Environment Variable(%JAVA_HOME%).
But java.home points to the JRE installation path, now it returns the JRE that was used to run the application, please remember that you can have multiple versions of JRE and JDK on the same server/computer
And you can run an application specifying what jre or jdk you want to use.
So, for example, if you have on your Environment path:
%JAVA_HOME% = C:\Program Files\Java\jdk1.6.0_24
But if you ran the application using an specific jre:
"C:\Program Files (x86)\Java\jre1.8.0_73\bin\java" -jar TheJavaFile.jar
Inside the application on run-time, you will get on java.home a different version of the JAVA_HOME
This may explain why on some cases you get different versions for both variable and system property.
Also, please notice that the paths may be quite different, since JRE is a different product than JDK, then they are installed in different locations, because they are independent
Now, regarding what's the difference from one JDK vs JRE, this diagram explains it pretty clear:
JDK is a superset of JRE, and contains everything that is in JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications. JRE provides the libraries, the Java Virtual Machine (JVM), and other components to run applets and applications written in the Java programming language.