How to Set Java Environment Path in Ubuntu Linux

Last updated January 31, 2026 11:05 am Joshua James 13 min read 2 comments

Setting the Java environment path on Ubuntu configures the JAVA_HOME variable, which tells development tools where to find the Java installation. Build automation tools like Apache Maven, web servers like Tomcat, and IDEs such as IntelliJ IDEA and Eclipse rely on this variable to locate the Java compiler, runtime libraries, and JDK tools. Without a properly configured JAVA_HOME, these applications fail to compile code, execute Java programs, or load necessary dependencies.

This guide demonstrates multiple approaches to setting JAVA_HOME permanently on Ubuntu, including system-wide configuration via /etc/environment for production servers, user-specific configuration via .bashrc for development environments, and dynamic methods that automatically track Java version changes. You will learn how to verify your configuration persists across terminal sessions, troubleshoot common PATH issues, and choose the method that best fits your workflow.

Install Java on Ubuntu

Check for an Existing Java Installation

Begin by determining if Java is already installed on your system. Open a terminal and run the following command:

java -version

If Java is already installed, the output displays version information. If Java is not installed, you will see:

Command 'java' not found, but can be installed with:
sudo apt install default-jre
sudo apt install openjdk-11-jre-headless
sudo apt install openjdk-17-jre-headless
sudo apt install openjdk-21-jre-headless

Install Java JDK on Ubuntu

If Java is not installed, you can install the Java Development Kit (JDK) using the following command:

sudo apt install default-jdk

This command installs the latest JDK available in the Ubuntu repository, a necessary component for Java development. On current Ubuntu releases, default-jdk maps to the latest supported OpenJDK. If you need a specific Java version, you can install OpenJDK 11, OpenJDK 17, or other versions from Ubuntu’s repositories.

Verifying Java Installation

After installation, it’s crucial to verify that Java is correctly installed. Run:

java -version

The output should display the installed Java version, confirming the installation’s success.

The command prints the Java runtime details, similar to:

openjdk 21.0.5 2024-10-15
OpenJDK Runtime Environment (build 21.0.5+11-Ubuntu-1ubuntu124.04)
OpenJDK 64-Bit Server VM (build 21.0.5+11-Ubuntu-1ubuntu124.04, mixed mode, sharing)

Setting the Java Environment Path

Understanding JAVA_HOME vs PATH: JAVA_HOME points to your Java installation directory (e.g., /usr/lib/jvm/java-21-openjdk-amd64), while PATH is a system variable that lists directories where the shell searches for executable commands. Most Java tools check JAVA_HOME to locate the JDK root, then append /bin to find binaries like javac and jar. When you add $JAVA_HOME/bin to PATH, you enable typing java directly instead of the full path /usr/lib/jvm/java-21-openjdk-amd64/bin/java.

What Is JAVA_HOME?

JAVA_HOME is an environment variable that stores the path to your Java installation directory. Build tools like Maven and Gradle read this variable to locate the Java compiler (javac), runtime libraries, and other JDK components. When JAVA_HOME is correctly set, these tools can function without requiring you to specify the full Java path in every configuration file or command.

Locating the Java Installation Path

If you’re uncertain about your Java installation path, you can locate it before setting the JAVA_HOME variable. Open a terminal and use the update-alternatives command to list all Java installations:

update-alternatives --list java

The output shows all installed Java versions and their full paths:

/usr/lib/jvm/java-11-openjdk-amd64/bin/java
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
/usr/lib/jvm/java-21-openjdk-amd64/bin/java

Identify the path corresponding to the Java version you want to use. For JAVA_HOME, remove the /bin/java suffix from the path. In this example, if you want to use Java 21, the JAVA_HOME path would be /usr/lib/jvm/java-21-openjdk-amd64.

If update-alternatives returns an error because no Java packages are installed yet, first install a JDK or use readlink -f $(command -v java) to locate the active binary.

Set Temporary JAVA_HOME

For a temporary setup, valid only in the current terminal session, define JAVA_HOME as follows:

export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64

Replace /usr/lib/jvm/java-21-openjdk-amd64 with the Java installation path you found. This temporary setting helps test and ensure that the Java environment is correctly configured before you make permanent changes. The variable persists only for the current terminal session and disappears when you close the terminal or log out.

Set Permanent JAVA_HOME

Permanent configuration ensures JAVA_HOME persists across terminal sessions and system reboots. Ubuntu offers four distinct methods for setting JAVA_HOME permanently, each suited to different use cases and system configurations.

Choose Your JAVA_HOME Configuration Method for Ubuntu

Select the configuration method based on whether you need system-wide or user-specific Java settings, and whether you switch Java versions frequently. The table below compares the four methods documented in this guide:

MethodScopeFile LocationPersistenceBest For
/etc/environmentSystem-wide/etc/environmentRequires logout or rebootProduction servers and multi-user systems where all users need the same Java configuration
~/.bashrcUser-specific~/.bashrcLoads on new terminal sessionsDevelopment workstations where different users need different Java versions
default-java symlinkSystem-wide/etc/environment.d/90java-default.confRequires logout or rebootSystems where Java updates should automatically use the latest default version
Dynamic scriptUser-specific~/.bashrc (one-line script)Loads on new terminal sessionsUsers who frequently switch Java versions with update-alternatives

For most users, the /etc/environment method is recommended because it provides consistent Java configuration across all users and services, making it reliable for production environments and shared systems. Use the .bashrc method if you need different Java versions per user account, the default-java symlink method if you want automatic version tracking after system updates, or the dynamic script if you regularly switch Java versions during development.

Method 1: System-Wide Configuration via /etc/environment

The /etc/environment file sets system-wide environment variables that apply to all users and services. This method is ideal for servers or multi-user systems where every user needs the same Java configuration.

Edit /etc/environment with nano

Open the environment file using nano:

sudo nano /etc/environment

In the nano editor, add:

JAVA_HOME="/usr/lib/jvm/java-x.x.x-openjdk"

Replace x.x.x with your Java version. Save and exit nano (CTRL + X, then Y and Enter).

Apply Changes Without Rebooting

The system reads /etc/environment only at login, so changes require logging out and back in, or rebooting. To apply the changes immediately in your current terminal session without logging out, run:

source /etc/environment

This command loads the updated environment variables into your current session. Note that other open terminals or running processes will not see the changes until they restart or you log out and back in.

Alternative: Set JAVA_HOME via /etc/environment.d/

Ubuntu 22.04 and later support the /etc/environment.d/ directory for modular environment configuration. You can keep JAVA_HOME in a dedicated file instead of editing /etc/environment directly:

echo 'JAVA_HOME="/usr/lib/jvm/java-x.x.x-openjdk"' | sudo tee /etc/environment.d/90java.conf > /dev/null

This avoids duplicate JAVA_HOME entries and simplifies removal. Reload your session after creating the file.

Add JAVA_HOME to PATH System-Wide

Because /etc/environment only stores name-value pairs, add PATH changes through a shell script so $JAVA_HOME expands correctly:

echo 'export PATH="$PATH:$JAVA_HOME/bin"' | sudo tee /etc/profile.d/java.sh > /dev/null

New login shells inherit the updated PATH after you log out and back in.

Verify System-Wide JAVA_HOME

After logging out and back in, verify that JAVA_HOME is set correctly:

echo $JAVA_HOME

The output shows your Java installation path:

/usr/lib/jvm/java-21-openjdk-amd64

Confirm that Java executables are accessible via the updated PATH:

which java
/usr/lib/jvm/java-21-openjdk-amd64/bin/java

Method 2: User-Specific Configuration via .bashrc

For user-specific Java configuration that does not affect other users or system services, add JAVA_HOME to your .bashrc file. This method is ideal for development workstations where different users may need different Java versions.

Open your .bashrc file with nano:

nano ~/.bashrc

Add these lines at the end of the file:

export JAVA_HOME="/usr/lib/jvm/java-x.x.x-openjdk"
export PATH=$PATH:$JAVA_HOME/bin

Replace x.x.x with your Java version. Save and exit nano (CTRL + X, then Y and Enter). Apply the changes to your current session:

source ~/.bashrc

The .bashrc method automatically loads JAVA_HOME every time you open a new terminal, without requiring system-wide changes or affecting other users.

Verify User-Specific JAVA_HOME

Confirm JAVA_HOME is correctly set in your current session:

echo $JAVA_HOME
/usr/lib/jvm/java-21-openjdk-amd64

Test that Java commands work without the full path:

java --version
openjdk 21.0.5 2024-10-15
OpenJDK Runtime Environment (build 21.0.5+11-Ubuntu-1ubuntu124.04)
OpenJDK 64-Bit Server VM (build 21.0.5+11-Ubuntu-1ubuntu124.04, mixed mode, sharing)

Method 3: Using the default-java Symlink

Ubuntu maintains a /usr/lib/jvm/default-java symlink that automatically points to the latest installed JDK. When you install Java through APT, this symlink updates to track the current default version. Using this path makes your configuration resilient to Java version upgrades:

echo 'JAVA_HOME="/usr/lib/jvm/default-java"' | sudo tee /etc/environment.d/90java-default.conf > /dev/null

This approach separates Java configuration from the main /etc/environment file, making it easier to manage and remove if needed. After creating the file, log out and back in to apply the changes system-wide.

To keep PATH in sync with this symlink, reuse the /etc/profile.d snippet from the system-wide method:

echo 'export PATH="$PATH:$JAVA_HOME/bin"' | sudo tee /etc/profile.d/java.sh > /dev/null

Verify Symlink Configuration

After logging out and back in, confirm the symlink points to your installed Java version:

readlink -f /usr/lib/jvm/default-java
/usr/lib/jvm/java-21-openjdk-amd64

Verify JAVA_HOME uses the symlink:

echo $JAVA_HOME
/usr/lib/jvm/default-java

Method 4: Dynamic JAVA_HOME for Version Switchers

If you frequently switch Java versions using update-alternatives, a dynamic JAVA_HOME script automatically tracks the currently active Java version without manual updates. Add this line to your ~/.bashrc:

jvm=$(update-alternatives --query java | grep '^Value:' | cut -f2 -d' ') ; export JAVA_HOME=${jvm%/*/*}

This one-liner queries update-alternatives for the currently selected Java executable path, extracts the full path using grep and cut, then stores it in the jvm variable. The parameter substitution ${jvm%/*/*} removes the trailing /bin/java portion from the path by stripping two directory levels from the end. For example, /usr/lib/jvm/java-21-openjdk-amd64/bin/java becomes /usr/lib/jvm/java-21-openjdk-amd64.

Understanding ${jvm%/*/*}: The % operator removes the shortest match of the pattern from the end of the variable. The pattern /*/* matches two path segments. This Bash parameter substitution technique is more efficient than using dirname twice or sed, and works reliably across all Ubuntu versions.

When you switch Java versions with update-alternatives --config java, JAVA_HOME automatically updates in new terminal sessions without editing configuration files. Apply the changes to your current session:

source ~/.bashrc

Verify Dynamic JAVA_HOME

Confirm the dynamic script correctly sets JAVA_HOME based on the active Java version:

echo $JAVA_HOME
/usr/lib/jvm/java-21-openjdk-amd64

Test version switching by selecting a different Java version and opening a new terminal:

sudo update-alternatives --config java

Select a different Java version from the menu, open a new terminal, and verify JAVA_HOME updated automatically:

echo $JAVA_HOME

The output should show the newly selected Java installation directory, confirming the dynamic configuration works as expected.

Verifying Java Environment Path Configuration

Validate your configuration by checking the JAVA_HOME and PATH variables:

echo $JAVA_HOME
echo $PATH

The first command displays your Java installation directory:

/usr/lib/jvm/java-21-openjdk-amd64

The second command shows your PATH variable, which should include $JAVA_HOME/bin if you added the PATH export:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-21-openjdk-amd64/bin

To confirm Java commands work correctly through JAVA_HOME, test the Java executable directly:

$JAVA_HOME/bin/java --version

The output shows the Java version and confirms JAVA_HOME points to a working Java installation:

openjdk 21.0.5 2024-10-15
OpenJDK Runtime Environment (build 21.0.5+11-Ubuntu-1ubuntu124.04)
OpenJDK 64-Bit Server VM (build 21.0.5+11-Ubuntu-1ubuntu124.04, mixed mode, sharing)

This verification step ensures both JAVA_HOME and PATH are correctly configured, allowing tools like Maven, Gradle, and IDEs to locate Java without issues.

Troubleshooting Common Issues

JAVA_HOME Returns Empty After Configuration

If JAVA_HOME returns empty after editing /etc/environment, you forgot to log out and back in or reload the configuration. Check the current value:

echo $JAVA_HOME

If the output is blank, the file loads only at login time, so existing terminal sessions do not see the changes. Reload the environment immediately in your current session:

source /etc/environment

Then verify JAVA_HOME again. For persistent issues, confirm the variable is correctly set in the file:

grep JAVA_HOME /etc/environment

The output should show your JAVA_HOME line:

JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"

If you set JAVA_HOME in /etc/environment.d/, check the file directly since source /etc/environment does not load that directory:

cat /etc/environment.d/90java.conf

The output confirms the file contains your JAVA_HOME configuration:

JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"

After confirming the value, log out and back in (or reboot) so the env.d file is applied to new sessions.

Java Commands Not Found Despite Correct JAVA_HOME

If JAVA_HOME is set correctly but java or javac commands still fail, your PATH variable does not include $JAVA_HOME/bin. Check if the Java binary directory is in your PATH:

echo $PATH | grep -o "$JAVA_HOME/bin"

If the PATH is configured correctly, the output shows:

/usr/lib/jvm/java-21-openjdk-amd64/bin

If the output is empty, $JAVA_HOME/bin is missing from PATH. For .bashrc configuration, ensure you added the PATH export line:

grep "PATH.*JAVA_HOME" ~/.bashrc

The output should show:

export PATH=$PATH:$JAVA_HOME/bin

For system-wide setups, verify the /etc/profile.d/java.sh file exists and contains the PATH update:

cat /etc/profile.d/java.sh

The file should contain:

export PATH="$PATH:$JAVA_HOME/bin"

If the file is missing or incorrect, recreate it using the command from the system-wide method so $JAVA_HOME expands correctly. Reload it in the current session:

source /etc/profile.d/java.sh

Otherwise, log out and back in for the PATH change to apply.

Multiple Java Versions Installed

When you have multiple Java versions installed, list all available versions:

update-alternatives --list java

The output shows all installed Java versions:

/usr/lib/jvm/java-11-openjdk-amd64/bin/java
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
/usr/lib/jvm/java-21-openjdk-amd64/bin/java

To select which version runs when you type java, use:

sudo update-alternatives --config java

This displays an interactive menu where you can select the default Java version. After switching versions with update-alternatives, update your JAVA_HOME path to match the new Java installation directory, or use the dynamic JAVA_HOME method to track version changes automatically.

Do Not Use export in /etc/environment

The /etc/environment file uses simple NAME=”value” syntax without the export keyword. If you accidentally added export, remove it:

sudo nano /etc/environment

Change this incorrect format:

export JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"

To the correct format:

JAVA_HOME="/usr/lib/jvm/java-21-openjdk-amd64"

Adding export JAVA_HOME to /etc/environment will not work because the file is parsed as name-value pairs, not executed as a shell script. Only .bashrc and .profile files require the export command before variable assignments.

Frequently Asked Questions

Why do I need to set JAVA_HOME on Ubuntu?

Development tools like Apache Maven, Gradle, Tomcat, and IDEs rely on the JAVA_HOME variable to locate the Java compiler and runtime libraries. Without it, these applications cannot build projects or execute Java code.

Does JAVA_HOME update automatically when I switch Java versions?

It depends on your configuration method. If you use the dynamic script or default-java symlink methods, JAVA_HOME automatically tracks the active version set by update-alternatives. If you hardcoded a specific path in /etc/environment, you must manually update the file when switching versions.

Should JAVA_HOME include the /bin directory?

No. JAVA_HOME must point to the Java installation root (e.g., /usr/lib/jvm/java-21-openjdk-amd64). The /bin directory acts as a subdirectory containing executables like java and javac. You only append /bin when adding Java to your system PATH.

What is the difference between /etc/environment and .bashrc for Java?

The /etc/environment file applies system-wide to all users and background services, making it ideal for production servers. The .bashrc file is user-specific and loads only when you open a terminal, which is safer for personal development environments or when multiple users need different Java versions.

Conclusion

Configuring JAVA_HOME ensures that build tools like Maven, application servers like Tomcat, and IDEs such as IntelliJ IDEA and Eclipse can locate your Java installation automatically. Ubuntu offers four configuration methods: system-wide /etc/environment for production servers, user-specific .bashrc for development environments, the default-java symlink for automatic version tracking, and dynamic scripts for frequent version switching. Choose the method that matches your workflow, verify JAVA_HOME persists across sessions, and update your configuration when installing or switching Java versions to maintain a functional development environment.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffee Buy me a coffee

2 thoughts on “How to Set Java Environment Path in Ubuntu Linux”

    • Thanks for reporting this, Doug. You are right that Mageia does not use /etc/environment. System-wide variables go in /etc/profile, while per-user settings belong in ~/.bashrc, so the Ubuntu-specific parts here do not directly apply to Mageia.

      For a system-wide Java setup on Mageia, add JAVA_HOME and update PATH in /etc/profile, then start a new login shell. For a user-only setup, put the same exports in your ~/.bashrc and source it. Examples:

      sudo nano /etc/profile

      export JAVA_HOME="/usr/lib/jvm/java-XX-openjdk"
      export PATH="$PATH:$JAVA_HOME/bin"

      source ~/.bashrc   # for user-specific setup

      Let me know if you would like a variant that auto-detects the active JVM.

      Reply
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags:

You type Result
<code>command</code> command
<pre>block of code</pre> code block
<strong>bold</strong> bold
<em>italic</em> italic
<a href="URL">link</a> link
<blockquote>quote</blockquote> quote block

Leave a Comment

We read and reply to every comment — let us know how we can help or improve this guide.

Let us know you are human: