The Java home directory is the installation directory for the Java Runtime Environment and Java Development Kit (see: Java JDK vs JRE. This directory contains all of the files that come with Java and are used to run Java programs or compile them. The location for this directory can change depending on your system (Linux, MacOS, Windows, etc.) and the version of Java that you have installed.
Since the directory is known to change across different systems and installations, Java programs that are trying to run or compile on your Linux system will need a reliable way to determine the location of the Java home directory. In Linux, the JAVA_HOME
environment variable is commonly used for applications to determine the correct location. Other methods exist, too – which you will see below.
In this tutorial, you will learn how to locate the Java home directory on a Linux system. We will also show how to set the Java home directory by editing the environment variable.
In this tutorial you will learn:
- How to locate JAVA home directory with environment variable, Java command, and basic Linux commands
- How to set the JAVA home directory via environment variable

Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | Java |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Locating the Java Home Directory on Linux
Let’s go over several methods below to see how we can determine the location of our Java home directory in Linux.
- The first method is to print the environment variable. With recent versions of Java, your system’s environment variable will probably only be set if you did it manually.
$ printenv JAVA_HOME or $ echo $JAVA_HOME
If you do not get any output, then your environment variable is not set, but you can use one of the other methods below.
- We can use Java itself to run a command that checks for the installation directory. Then, we use the grep command to look for the pertinent setting (in this case,
java.home
).$ java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home' java.home = /usr/lib/jvm/java-19-openjdk-amd64
The command shows us exactly where Java home is located.
DID YOU KNOW?
If this command produces an error for you, then you either do not have Java installed at all, or it has been misconfigured on your system. We recommend using the find command to manually find your Java files, or simply reinstall the Java JRE or JDK on your system. - Another way to determine the Java home directory is by using a combination of the
dirname
and other commands below. This works by uncovering the root directory for thejavac
executable.$ dirname $(dirname $(readlink -f $(which javac))) /usr/lib/jvm/java-19-openjdk-amd64
- Yet another way would be to check the path for your
java
command. We can use thewhich
command for this. The only tricky part here is that the command will often refer to a symbolic link. And, possibly, that symbolic link may refer to yet another. Take a look at the output below that we took from our test system in order to see what we mean.$ which java /usr/bin/java
The
java
command points to/usr/bin/java
– let’s see where that goes.$ ls -l /usr/bin/java lrwxrwxrwx 1 root root 22 May 22 23:43 /usr/bin/java -> /etc/alternatives/java
It points to another symbolic link. Further investigation yields the location of the Java executable and therefore the Java home directory:
$ ls -l /etc/alternatives/java lrwxrwxrwx 1 root root 43 Jun 6 01:50 /etc/alternatives/java -> /usr/lib/jvm/java-19-openjdk-amd64/bin/java
The Java home directory is revealed as
/usr/lib/jvm/java-19-openjdk-amd64
on our test system. The Java executable itself resides atbin/Java
, but take note that this part is not the home directory.
Setting the Java Home Directory on Linux
We will set the Java home directory on our Linux system by configuring the environment variable for JAVA_HOME
.
- Open your user’s
~/.bashrc
file in nano or your preferred text editor.$ nano ~/.bashrc
- At the bottom of this file, add the following line:
export JAVA_HOME='/usr/lib/jvm/java-19-openjdk-amd64'
Be sure to replace the directory above with your actual Java home directory (where all of your Java JDK and JRE files reside).
- Afterwards, save changes and exit the file. Then, run the following command in terminal for the changes to take effect:
$ source ~/.bashrc
All done.
Closing Thoughts
In this tutorial, we saw how to locate and set the JAVA home directory on a Linux system. We learned how to check the environment variable for the home directory, use a Java command to uncover the home directory, and manually determine the home directory through tracing the path to the Java executable files. One of these methods will surely work for any Linux environment, assuming you have installed Java correctly to begin with. We also learned how to set the JAVA_HOME directory environment variable.