what is the reason for the existence of the JAVA_HOME environment variable?

85k Views Asked by At

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

8

There are 8 best solutions below

0
On

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.

3
On

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.

4
On

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

3
On

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.

0
On

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

0
On

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.

0
On

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.

0
On

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.