Difference between java.home and JAVA_HOME
Asked Answered
G

2

21

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.

Gangrel answered 1/8, 2017 at 15:10 Comment(0)
S
19

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:

enter image description here

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.

Swann answered 1/8, 2017 at 15:48 Comment(0)
G
5

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.

Gilford answered 1/8, 2017 at 15:36 Comment(3)
It looks the question itself has this answer: JAVA_HOME = jdk installation path and java.home = jre installation path... hence, it seems the question which needs to be answer here is: Why can't jre path match inside jdk path? in which the answer could be as simple as "They are different and independent products, hence they don't necessary share the same path"Swann
@MarcoVargas - You are right. But my question was - jdk = C:/Program Files/Java/jdk1.8.0_112 and then why can't jre be C:/Program Files/Java/jdk1.8.0_112/jre ?Gangrel
Agree with you, that´s why I posted the above comment, since Jay suggested to mark this answer as the definitively answer, but it seems your question was answered in the other answer I gave... stating that it is because they are different Oracle Products, that´s why they don´t share the root pathSwann

© 2022 - 2024 — McMap. All rights reserved.