HOWTO · Java

How to Set JAVA_HOME Environment Path in Ubuntu

Learn how to find the active Java installation on Ubuntu, set JAVA_HOME for your shell, make it persistent, and verify the result.

On this page

JAVA_HOME is an environment variable that points to the root directory of a Java installation. It should identify the JDK or JRE directory that contains bin, not the java executable itself and not the bin directory. Some build tools and Java applications use this variable to locate the installation. The instructions below find the Java command your shell currently selects, set the variable for the current shell, and show how to save it for later Bash sessions.

Check the Java installation

Open a terminal and run java -version. If the shell prints a Java version, a Java runtime is available through the current PATH. The exact version depends on what is installed and selected on this machine; no particular version is required by these steps. If the command is not found, install the JDK version your project needs with Ubuntu’s package manager, then continue. A JDK includes development tools such as javac; a runtime-only installation may not.

To see which executable the shell will use, run command -v java. This reports the command’s path, which may lead through symbolic links managed by the system. The path can change when you switch Java alternatives, so do not copy a version-specific directory from another machine.

Find the Java home directory

Resolve the selected command’s symbolic links with readlink -f "$(command -v java)". In a standard JDK layout, the resolved executable ends in bin/java; its installation root is two directories above that file. You can derive the root in Bash with dirname "$(dirname "$(readlink -f "$(command -v java)")")". Inspect the result before saving it: it should be the installation directory containing bin, rather than a path ending in /bin/java or /bin.

If more than one Java installation is present, command -v java and the resolved path identify the one selected for this shell. Ubuntu systems commonly manage alternatives for the java command. A selected runtime and a separately selected compiler can differ, so if your project requires a JDK, check javac -version as well and confirm that the chosen installation contains the development tools you need. Choose the intended JDK root when configuring JAVA_HOME.

Set JAVA_HOME in the current shell

After determining the correct root, assign that directory to JAVA_HOME and prepend its bin directory to PATH. These changes affect only the current shell and programs started from it. Replace the example path with the root you found; the path below is illustrative and is not a claim about Ubuntu’s installation layout.

Run this example:

JAVA_HOME=/path/to/your/jdk
export JAVA_HOME
PATH="$JAVA_HOME/bin:$PATH"
export PATH

The first export makes the variable available to child processes. The PATH assignment puts this installation’s Java tools ahead of other matching commands, so a shell started from this one will ordinarily find this installation first. If you only need an application to use JAVA_HOME, you may not need to change PATH; if you do change it, avoid adding the same directory repeatedly in scripts that run more than once.

Make JAVA_HOME persistent on Ubuntu

For Bash, put the assignments in the startup file used by the shell context where you need them. Interactive Bash terminals commonly read ~/.bashrc. Login shells read a login startup file such as ~/.profile (or ~/.bash_profile when present); Ubuntu’s default profile may also load .bashrc. Use the file appropriate to how you launch the shell rather than assuming that one file is read by every terminal, remote session, or service. Zsh users should configure their Zsh startup file, typically ~/.zshrc, instead of editing Bash configuration.

Add the two export assignments from the example, replacing the placeholder with the verified JDK root. Edit the chosen file with your preferred text editor, save it, and start a new shell to load the setting. To apply a Bash file immediately to the current Bash session, run source ~/.bashrc or source ~/.profile for the file you edited. Sourcing a file evaluates its commands in the current shell; it does not update already-running applications or unrelated shells. Services and graphical applications may have their own environment setup, so shell startup files do not guarantee that every launcher inherits the value.

Verify the configuration and troubleshoot common issues

In the shell where you configured the variable, inspect it with printf '%s\n' "$JAVA_HOME". Confirm that the displayed directory is the JDK root and contains a bin directory. Then run command -v java and java -version; the selected command should resolve under the installation whose bin directory you placed first in PATH. If you need the compiler, check command -v javac and javac -version too.

If JAVA_HOME is blank in a new terminal, the startup file you edited may not be read in that kind of session, or the file may contain a syntax error. Identify the shell running this session with ps -p $$ -o comm=; this reports the current shell process even when it differs from the configured login shell in $SHELL. Confirm the export lines are in the matching startup file, and open a new shell or source that file again. If the variable ends in /bin or /bin/java, change it to the installation root. If java -version reports a different installation, inspect PATH and the resolved command with command -v java and readlink -f "$(command -v java)"; another directory may appear earlier, or the system alternative may select a different Java. Update JAVA_HOME and PATH to the intended JDK, then repeat these checks in the same shell. Once the values and selected commands point to the intended installation, the configuration is ready for tools launched from that shell.