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.
PATH values: notice how the directory we set for JAVA_HOME is the JDK installation root whereas for PATH we add the bin directory within the JDK installation. Take care to set these up correctly otherwise you'll have problems later on.
JAVA_HOME is an operating system (OS) environment variable which can optionally be set after either the Java Development Kit (JDK) or the Java Runtime Environment (JRE) is installed. The JAVA_HOME environment variable points to the file system location where the JDK or JRE was installed.
If you're doing any sort of development, or building with Maven or Ant, you need to point to the JDK (Java Development Kit) where utilities such as javac (the Java Compiler) reside. Otherwise, you can point to the JRE (Java Runtime Environment). The JDK contains everything the JRE has and more.
The JRE_HOME variable is used to specify location of a JRE. The JAVA_HOME variable is used to specify location of a JDK. Using JAVA_HOME provides access to certain additional startup options that are not allowed when JRE_HOME is used. If both JRE_HOME and JAVA_HOME are specified, JRE_HOME is used.
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.
According to Oracle documentation java.home
is a system property that represents the installation directory of Java Runtime Environment. JAVA_HOME
is an environment variable - not a system property. Please refer to this post to check what is the difference between system property and environment variable.
You can also refer to this post to learn more about JAVA_HOME
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With