1. Overview

In this tutorial, we’ll explore the key difference between using JAVA_HOME and PATH environment variables. Though most Java programs need both these variables to compile and run successfully, each serves a different purpose. Let’s see them one by one.

2. JAVA_HOME Environment Variable

The JAVA_HOME environment variable points to the JDK installation directory. Subsequently, other Java-dependent programs can use this variable to access the JDK/JRE path.

In general, Apache Tomcat and other Java EE application servers, along with build tools such as Maven or Gradle, use JAVA_HOME as a naming convention to locate Java.

In most cases, JAVA_HOME is set to point to the JDK path instead of the JRE. This is useful for modern development tools that need access to the compiler, debugger, document generator, etc.

Here’s how we set the JAVA_HOME variable through the command-line:

set JAVA_HOME=C:\Program Files\Java\jdk-17

Alternatively, we can also set this variable through system settings. Windows users can find this setting under the System Properties -> Advanced -> Environment Variables.

3. PATH Environment Variable

The operating system uses the PATH environment variable to find the native executable programs to run.

In Windows, the executable programs end with a .exe extension. Once we point the executable program’s directory in PATH, we can invoke the same in the command-line without the need to specify its full path.

For Java programs to run, we need to list the JDK installation directory along with the bin directory in the PATH variable. The bin directory holds the Java executables.

We can set the PATH environment variable through the command-line:

set PATH=C:\Program Files\Java\jdk-17\bin

As seen in the previous section, we can achieve the same through the system settings. Typically, the PATH variable contains multiple paths pointing to different directories. As such, this setting is not limited to Java.

4. Using JAVA_HOME and PATH Together

Modern programs are intelligent enough to pick one of the JAVA_HOME or PATH variables to run successfully. However, a few other programs, such as Eclipse, need the PATH variable to startup.

As a best practice, setting both the JAVA_HOME and PATH environment variables is always recommended. This way, we can remove the compatibility issues between using old and new programs.

Also, to avoid repeating the JDK installation path twice, we can reuse one of these variables in the declaration:

set PATH=%JAVA_HOME%\bin;%PATH%

As a result, this setting also removes the risk of changing all the affected environment variables when there’s a change in the JDK installation directory.

5. Common Problems

Certain programs rely on specific variables for startup. For instance, if JAVA_HOME isn’t set, the programs that rely on this variable may not locate the JDK.

Subsequently, programs such as Tomcat, Maven, and Gradle may not execute properly. Likewise, the same issue applies to the programs which rely on the PATH variable.

Specifically, when we try to run a Java program from the command-line, the operating system looks into the PATH variable to locate and run the JVM. If there’s no JDK on the PATH variable, the command terminates with an error:

C:\Users\palan>java HelloWorld
'java' is not recognized as an internal or external command,
operable program or batch file.

Another common issue is having different JDK versions in the PATH variable. In such a case, the operating system considers the first occurrence of JDK in the PATH variable for execution. However, it’s better to remove multiple versions in the PATH variable to avoid confusion at a later stage.

6. Conclusion

In this article, we explored the differences between using the JAVA_HOME and PATH environment variables.

To conclude, the JAVA_HOME environment variable is used by programs mostly targeted as development tools. On the other hand, user-facing applications need the PATH environment variable to know the location of the JVM.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.