Grafvision - Fotolia

How to set JAVA_HOME in Windows and echo the result

Set JAVA_HOME properly and echo your results with the following tidbits, and also examine how a proper PATH installation can benefit your local environment.

If you plan to run software programs like Maven, Jenkins, Gradle or Tomcat, you'll not only need a local installation of the JDK, but you'll also want to ensure that you have set JAVA_HOME correctly. Here is how you can accomplish that task with a demonstration of how to set JAVA_HOME in Windows and echo the result to validate that the changes have gone into effect.  Also, let's explore how you can add Java to the Windows PATH, which is another configuration parameter that is often set after a JDK installation.

How to set JAVA_HOME in Windows

Three ways to set the JDK JAVA_HOME variable in Windows include:

  • allow the Java installer to do it;
  • use the environment variable editor; and
  • use the command line.

OpenJDK's JAVA_HOME configuration tool

Not every Java installer will automatically set JAVA_HOME for you. The AdoptOpenJDK one will, and it can configure the PATH variable for you as well. But, it doesn't do it all by default. You must perform a custom install and select one of the following two options to have the OpenJDK installer set JAVA_HOME and PATH environment variables:

  • add to PATH; or
  • set JAVA_HOME variable.
set JAVA_HOME OpenJDK
The OpenJDK JAVA_HOME setup tool greatly simplifies configuration.

That's all there is to it. Once the OpenJDK installation is complete, the JAVA_HOME variable will be configured and the bin directory of the JDK will be added to the Windows PATH. It doesn't get much easier than that.

The JAVA_HOME environment variable editor

If your JDK installation didn't set JAVA_HOME automatically, you can always open the Windows environment variable editor and set it yourself. This is the easiest way to manually set JAVA_HOME in Windows 7, 8 and 10.  

Follow these steps to manually set JAVA_HOME:

  • Open the Windows System Properties Control Panel applet on any version of Windows.
  • Choose Advanced System settings.
  • Click on the Environment Variables button.
  • Click on the New button under System Variables.
  • Set JAVA_HOME as the environment variable name.
  • Set the location of the JDK installation as the environment variable Value.
  • Click OK and close the JAVA_HOME environment variable editor.
Windows 7 JAVA_HOME
How to use the JAVA_HOME environment variable editor in Windows 7.

After you add the new environment variable, close any and all DOS prompts and command windows, because these tools load environment variables only when they first start. If you try to access the JAVA_HOME variable in any windows that were open prior to the change, the variable will come back as null or undefined. But if you open a new command window, scripts that search for JAVA_HOME will run successfully.

The setx JAVA_HOME command

The third way to configure the Windows JAVA_HOME environment variable is to use the command line and invoke the setx command as follows: 

@REM Configure JAVA_HOME in Windows 10 with setx
setx JAVA_HOME -m "C:\_jdk12.0"
>> setx JAVA_HOME command completed successfully 

As you can see, the setx JAVA_HOME approach is relatively simple. Still, command line interface tools can intimidate some people, and this type of manual coding is prone to error. However, when you write scripts to automate the configuration of the environment, the ability to script the process with setx becomes invaluable.

How to echo JAVA_HOME in Windows

After you set JAVA_HOME in Windows, it's a good idea to verify that the change has persisted. The easiest way to do this is to echo JAVA_HOME in a command prompt or a BASH shell. 

To echo JAVA_HOME in a Windows DOS prompt or command window, you simply bookend the variable with percentage signs:  

@REM How to echo JAVA_HOME in windows
echo %JAVA_HOME%
>> C:\_jdk12.0 

JAVA_HOME Ubuntu

If you use a BASH shell, or if you have set JAVA_HOME in an Ubuntu environment and need to echo JAVA_HOME in Linux, place a single dollar sign before the environment variable: echo $JAVA_HOME.

How to get JAVA_HOME in Windows scripts

The manner in which you get JAVA_HOME within batch files and shell scripts follows the exact same syntax used by the echo command.  Bookend the variable with percentage signs and use that variable within your code just as you would any other scripted variable. 

Scripts that use JAVA_HOME should always be checked to see if the variable exists. If it does not, an appropriate error message will arise. Here is how the Apache Maven project gets JAVA_HOME in its startup script and reports any errors during the process: 

@REM Apache Maven JAVA_HOME Startup Script
@REM ==START JAVA_HOME VALIDATION ==
if not "%JAVA_HOME%"=="" goto OkJHome
for %%i in (java.exe) do set "JAVACMD=%%~$PATH:i"
goto checkJCmd

:OkJHome
set "JAVACMD=%JAVA_HOME%\bin\java.exe"

:checkJCmd
if exist "%JAVACMD%" goto chkMHome

echo JAVA_HOME environment variable is not defined correctly >&2
echo This environment variable is needed to run this program >&2
echo NB: JAVA_HOME should point to a JDK not a JRE >&2
goto error

The JAVA_HOME PATH setting in Windows

The JAVA_HOME and PATH environment variables serve two very different purposes. JAVA_HOME simply points to where Java is installed. If you add something to the PATH variable, it makes it available throughout the entire operating system. Of course, many developers who install Java actually want the runtime universally available, so they set the JAVA_HOME and PATH environment variables at the same time. 

The big distinction between PATH and JAVA_HOME settings is that the former points to the JDK bin directory, while the latter points to the installation directory. Developers are notorious for mixing up these two settings, which invariably leads to program start issues and the subsequent JAVA_HOME error messages such as "java_home is set to an invalid directory" or "java_home environment variable is not set."

Java PATH variable
How to add the JDK bin directory to the Windows PATH.

The JAVA_HOME bin combination

When you add the JDK \bin directory to the PATH, you can specify the absolute path, or you can get clever and reference the JAVA_HOME environment variable as so: 

%JAVA_HOME% bin

How to set JRE_HOME in Windows

It is worth noting that while a JDK installation is linked to the JAVA_HOME environment variable, JRE installations are typically linked to the JRE_HOME variable. The steps to set up JRE_HOME on a Windows machine are exactly the same as those outlined above, with the exception that the JRE_HOME variable will point to the root of the JRE installation, while the JAVA_HOME environment variable in Windows points to the root of the JDK installation.

 

Dig Deeper on Core Java APIs and programming techniques

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close