111

Many java based application requires to set JAVA_HOME env variable. What's the purpose of this variable?

Thufir
  • 8,216
  • 28
  • 125
  • 273
user496949
  • 83,087
  • 147
  • 309
  • 426
  • 5
    you can read [here](http://stackoverflow.com/questions/2025290/what-is-java-home-how-does-jvm-will-find-the-javac-path-stored-in-java-home) – Sergey Vedernikov Feb 24 '11 at 08:21
  • 3
    I find the question has not an answer yet. As to execute java itself, it's not necessary to have this variable if you have the folder with the binary in the path. All answers point out the obvious: "Where java was installed". But Why: what is the meaning to running java applications? – Wolf Jul 30 '16 at 06:18
  • 7
    despite all these comments and answers, I see no actual **reason** to set this environment variable. No rationale. I came here from trying to set this with wsdl2java. If I wasn't using this tool, no need for the env variable....seemingly it's just that some tools use this var. but why? – Thufir Apr 24 '17 at 22:19
  • @Thufir - I have not seen the **reason** stated anywhere explicitly, however. - JAVA_HOME is just convention. It doesn't have any special meaning other than everyone agreed to use it AFAIK - The only use I see is it gives tools the ability to differentiate between the JRE which is normally set in the PATH variable and the JDK. Normally the JAVE_HOME is set to the JDK. - It's explicit. There is no dealing with the order of directories like you have with PATH where there could be multiple places to find java exe. – Rohan Jul 12 '18 at 02:02

8 Answers8

24

The purpose is to point to where Java is installed. $JAVA_HOME/bin/java should execute the Java runtime.

Wolf
  • 9,679
  • 7
  • 62
  • 108
Haakon
  • 1,741
  • 10
  • 19
  • 10
    But it is stupid, because java should be in the path, and the binary can locate itself and open the other files using a relative path. – LtWorf Dec 01 '14 at 10:57
  • 9
    It's useful when java is *not* on $PATH, for example when you have multiple SDK installations. – Haakon Dec 01 '14 at 15:14
  • The 1st argument to the binary is the full name of the executable itself, java doesn't need to be in the path to find its own directory. – LtWorf Dec 09 '14 at 13:41
  • @Haakon As someone trying to set up multiple JDKs, why would you *not* want java on the path? How does Windows locate java from java_home anyway? I thought the path was more or less defined as "the list of places the OS will look for executables". – LastStar007 Feb 07 '19 at 05:34
22

Environment variables are strings that contain information such as drive, path, or file name.

The JAVA_HOME environment variable points to the directory where the Java runtime environment (JRE) is installed on your computer.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
kamaci
  • 72,915
  • 69
  • 228
  • 366
  • 46
    One correction: JAVA_HOME should point to a JDK not a JRE. Many apps don't care about the difference but it is significant for some. – Chris Nava Feb 24 '11 at 16:37
  • 4
    There is also JRE folder available in the JDK. Hence, with the help of JAVA_HOME , an application can locate JDK as well as JRE. – rogue lad Jan 03 '14 at 10:42
  • @ChrisNava: Seem that pointing JAVA_HOME to a JRE installation is valid in some cases (most non-development cases I would guess): https://stackoverflow.com/questions/17601827/which-one-should-java-home-to-point-jdk-or-jre. So saying "should" is saying too much. – cic Jan 20 '18 at 19:11
8

When you run a Java program you need to first start the JVM, typically this is done by running an executable, on Windows this is java.exe. You can get that in lots of ways for example just giving a full path:

C:\somedir\bin\java.exe

or may having it on your PATH.

You specify as command line arguments some class files or Jar files containing class files which are your program. But that's not enough, the java.exe itself needs various other resources, not least all the Java libraries. By setting the environment variable JAVA_HOME you specify where the JRE, and hence all those runtime resources, are to be found. You should ensure that the particular Java you execute matches the setting of JAVA_HOME.

You may also need to put JAVA_HOME\bin early on your path so that if the java.exe spawns a copy of itself (for example to compile a JSP) it picks up the right version.

Wolf
  • 9,679
  • 7
  • 62
  • 108
djna
  • 54,992
  • 14
  • 74
  • 117
8

According to @dirai here JAVA_HOME is not used by Java itself but by some third party tools like for instance Apache Tomcat. Most applications however will work without this variable being set but some could show unexpected behaviour.

karlsebal
  • 1,449
  • 17
  • 23
5

JAVA_HOME allows you to run any of multiple java installations. Even if you have the /usr/lib/jvm/java-1.8.0/bin/java in your PATH first, it's useful to be able to say JAVA_HOME=/usr/lib/jvm/java-1.5.0 and have the default "java" with which you run programs use the other one, instead, as a matter of an environment setting. This allows a programmer to use the java they need, even if it's not installed in a normal system location, and test or provide reverse compatibility with that alternate version. And it's not vulnerable to being reset in the PATH by another installed program, such as a major version update for java itself.

Graham
  • 7,431
  • 18
  • 59
  • 84
5

JAVA_HOME is an environment variable.

It has to be setup on different operating system. Check out the following links.

Also see here for more information.

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
  • 1
    You are conflating `environment` with `path`, which are different things but used in Windows exactly the same way as in Linux. – Val Oct 13 '13 at 10:39
  • 1
    @val just pointing out, that has since been corrected in his answer. – barlop Jul 27 '14 at 12:28
  • Just to add to the list [JAVA_HOME in Windows 10](https://javatutorial.net/set-java-home-windows-10) – filip_j Aug 18 '16 at 10:00
2

To understand the purpose of JAVA_HOME environment variable, you need to first understand which programs look for and read this variable value. Some examples are: Apache Tomcat, Apache Maven, Gradle, Jenkins etc. If you notice, most of these are development tools and that's why mostly JAVA_HOME is set to point to JDK instead of JRE.

Consumer facing Java applications don't read JAVA_HOME variable and they just need to know where the JVM is located and that's why JVM location (directory) needs to be added to the PATH variable. This is automatically done for you when you install Java software. Whenever you try to run a Java application by either double clicking on the app or through command line, your operating system reads PATH variable to locate and run JVM and it doesn't look for JAVA_HOME variable.

Pavan
  • 632
  • 5
  • 9
0

JAVA_HOME is used in MacOS to determine which JRE to use when you are trying to launch java, javac, etc. java executable on PATH is just a proxy which then launches java from JAVA_HOME

Ivan
  • 87
  • 1
  • 6