How to Set Java Environment Path in Ubuntu 26.04, 24.04 and 22.04

Set Java environment path in Ubuntu 26.04, 24.04 and 22.04 using JAVA_HOME. Includes system-wide, user-specific, and dynamic methods.

Last updatedAuthorJoshua JamesRead time6 minGuide typeUbuntuDiscussion2 comments

Java tools on Ubuntu usually fail in confusing ways when JAVA_HOME is missing: Maven cannot find the compiler, Gradle may select the wrong runtime, and application servers can start with a different JDK than the one you expected. To set the Java environment path on Ubuntu, first identify the JDK root directory, then export JAVA_HOME and add its bin directory to PATH only where that scope makes sense.

The commands apply to Ubuntu 26.04 (resolute), 24.04 (noble), and 22.04 (jammy). The system-wide method fits shared development hosts and login sessions, while the user-specific method is safer when one account needs its own Java version. If you are preparing a Java build workflow, the Apache Maven installation guide for Ubuntu is a useful next step after JAVA_HOME is set.

Set JAVA_HOME on Ubuntu

Start by checking whether the java command already exists:

java --version

If Java is installed, the first line identifies the active runtime. A current Ubuntu 26.04 default JDK example starts like this:

openjdk 25.0.3-ea 2026-04-21
OpenJDK Runtime Environment (build 25.0.3-ea+7-Ubuntu-2)
OpenJDK 64-Bit Server VM (build 25.0.3-ea+7-Ubuntu-2, mixed mode, sharing)

If the command is missing, refresh APT metadata before installing the default JDK package:

sudo apt update

These commands use sudo because installing packages and writing files under /etc require administrative privileges. If your account cannot use sudo yet, follow the guide to add a new user to sudoers on Ubuntu before continuing.

Install the default JDK, which includes both the Java runtime and development tools such as javac, jar, and javadoc:

sudo apt install default-jdk

The default-jdk package resolves to the default OpenJDK branch for your Ubuntu release:

Ubuntu releasedefault-jdk branchDefault JDK root
Ubuntu 26.04 (resolute)OpenJDK 25/usr/lib/jvm/java-25-openjdk-amd64
Ubuntu 24.04 (noble)OpenJDK 21/usr/lib/jvm/java-21-openjdk-amd64
Ubuntu 22.04 (jammy)OpenJDK 11/usr/lib/jvm/java-11-openjdk-amd64

Use a version-specific OpenJDK package only when your project requires it. LinuxCapable also has separate guides to install OpenJDK 11 on Ubuntu, install OpenJDK 17 on Ubuntu, install OpenJDK 21 on Ubuntu, and install OpenJDK 25 on Ubuntu when the default package is not the branch you need.

Find the Active Java Home Path

Use readlink against /usr/bin/java to follow Ubuntu’s update-alternatives symlink and return the real JDK path:

readlink -f /usr/bin/java

The output points to the active Java binary:

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

JAVA_HOME should point to the JDK root, not the bin/java executable. Strip the final /bin/java path components automatically with this command:

JAVA_HOME_PATH="$(dirname "$(dirname "$(readlink -f /usr/bin/java)")")"
echo "$JAVA_HOME_PATH"

The result is the directory that belongs in JAVA_HOME:

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

Understand JAVA_HOME vs PATH

JAVA_HOME names the JDK root directory, such as /usr/lib/jvm/java-21-openjdk-amd64. PATH is the shell search list for commands. Build tools read JAVA_HOME to find the compiler and runtime libraries, while adding $JAVA_HOME/bin to PATH lets you run tools such as java, javac, and jar without typing their full paths.

Do not set JAVA_HOME to a path ending in /bin or /bin/java. Tools expect the installation root and add their own /bin lookup when needed.

Use default-java, Not default-jvm, on Ubuntu

Ubuntu’s default-jdk package creates the symlink /usr/lib/jvm/default-java. On Ubuntu 26.04, 24.04, and 22.04, the Alpine-style /usr/lib/jvm/default-jvm symlink is not created by the default JDK packages. Use /usr/lib/jvm/default-java when you want JAVA_HOME to follow Ubuntu’s default JDK package branch.

readlink -f /usr/lib/jvm/default-java

On Ubuntu 24.04, the symlink resolves to the default OpenJDK 21 directory:

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

Set JAVA_HOME System-Wide on Ubuntu

Use the system-wide method when every login session should see the same Java configuration. This is the cleanest option for shared development machines, build users, and accounts that should follow Ubuntu’s default JDK branch through /usr/lib/jvm/default-java.

Create a system-wide environment file for sessions that read systemd environment snippets:

printf 'JAVA_HOME=/usr/lib/jvm/default-java\n' | sudo tee /etc/environment.d/90-java-home.conf > /dev/null

The sudo tee form writes the file as root; a plain shell redirect would still run as your user and fail under /etc. Add a profile script so new shell sessions also put Java tools on PATH without adding duplicate entries each time the shell starts:

sudo tee /etc/profile.d/java-home.sh > /dev/null <<'EOF'
export JAVA_HOME="/usr/lib/jvm/default-java"
case ":$PATH:" in
  *":$JAVA_HOME/bin:"*) ;;
  *) export PATH="$JAVA_HOME/bin:$PATH" ;;
esac
EOF

Make the profile script readable:

sudo chmod 644 /etc/profile.d/java-home.sh

Log out and back in so Ubuntu applies the new environment to fresh sessions. To load the shell profile in the current terminal only, run:

. /etc/profile.d/java-home.sh

Systemd services do not automatically inherit shell profile files. If a service such as Tomcat needs JAVA_HOME, set it in that service’s unit file or a systemd drop-in instead of relying on /etc/profile.d/java-home.sh.

Verify System-Wide JAVA_HOME

Check the variable in a new terminal or after sourcing the profile script:

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

Confirm the Java binary under JAVA_HOME works:

"$JAVA_HOME/bin/java" --version

Relevant output on Ubuntu 24.04 starts with OpenJDK 21:

openjdk 21.0.10 2026-01-20
OpenJDK Runtime Environment (build 21.0.10+7-Ubuntu-124.04)
OpenJDK 64-Bit Server VM (build 21.0.10+7-Ubuntu-124.04, mixed mode, sharing)

Set JAVA_HOME for One Ubuntu User

Use the user-specific method when only your account should receive JAVA_HOME, or when you do not want to write files under /etc. The following block replaces only the managed Java section in ~/.bashrc, so rerunning it does not keep stacking duplicate exports.

touch "$HOME/.bashrc"
sed -i '/^# LinuxCapable JAVA_HOME start$/,/^# LinuxCapable JAVA_HOME end$/d' "$HOME/.bashrc"
cat >> "$HOME/.bashrc" <<'EOF'

# LinuxCapable JAVA_HOME start
export JAVA_HOME="/usr/lib/jvm/default-java"
case ":$PATH:" in
  *":$JAVA_HOME/bin:"*) ;;
  *) export PATH="$JAVA_HOME/bin:$PATH" ;;
esac
# LinuxCapable JAVA_HOME end
EOF

The sed command removes any previous managed block before the new one is appended. If you want to adapt this pattern for other shell variables, the sed command examples for Linux explain the matching and replacement syntax in more detail.

Apply the change to the current interactive shell, or open a new terminal:

. "$HOME/.bashrc"

Verify the result:

echo "$JAVA_HOME"
command -v javac
/usr/lib/jvm/default-java
/usr/lib/jvm/default-java/bin/javac

Track update-alternatives with JAVA_HOME

The /usr/lib/jvm/default-java symlink follows Ubuntu’s default JDK package, not every manual Java switch you make. If you use sudo update-alternatives --config java to switch between installed versions, use a dynamic shell block that derives JAVA_HOME from /usr/bin/java each time a new terminal opens.

touch "$HOME/.bashrc"
sed -i '/^# LinuxCapable JAVA_HOME start$/,/^# LinuxCapable JAVA_HOME end$/d' "$HOME/.bashrc"
cat >> "$HOME/.bashrc" <<'EOF'

# LinuxCapable JAVA_HOME start
if [ -x /usr/bin/java ]; then
  export JAVA_HOME="$(dirname "$(dirname "$(readlink -f /usr/bin/java)")")"
  case ":$PATH:" in
    *":$JAVA_HOME/bin:"*) ;;
    *) export PATH="$JAVA_HOME/bin:$PATH" ;;
  esac
fi
# LinuxCapable JAVA_HOME end
EOF

Apply the block to your current shell:

. "$HOME/.bashrc"

Switch the active Java version when multiple JDKs are installed:

sudo update-alternatives --config java

After selecting a different Java path, open a new terminal and verify that JAVA_HOME changed with the active alternative. Because the dynamic block prepends $JAVA_HOME/bin, tools such as javac and jar come from the same JDK directory as the selected java binary.

echo "$JAVA_HOME"
readlink -f /usr/bin/java
/usr/lib/jvm/java-17-openjdk-amd64
/usr/lib/jvm/java-17-openjdk-amd64/bin/java

Troubleshoot JAVA_HOME on Ubuntu

JAVA_HOME Is Empty in a New Terminal

If JAVA_HOME is blank after using the system-wide method, confirm that the environment file exists and contains the expected assignment:

if [ -f /etc/environment.d/90-java-home.conf ]; then
  cat /etc/environment.d/90-java-home.conf
else
  echo "/etc/environment.d/90-java-home.conf is not present"
fi

The expected system-wide line looks like this:

JAVA_HOME=/usr/lib/jvm/default-java

If you used the ~/.bashrc method, confirm that the managed block is present:

if [ -f "$HOME/.bashrc" ]; then
  grep 'LinuxCapable JAVA_HOME' "$HOME/.bashrc" || echo "No LinuxCapable JAVA_HOME block in $HOME/.bashrc"
else
  echo "$HOME/.bashrc is not present"
fi

For /etc/environment.d/ changes, log out and back in. For ~/.bashrc changes, open a new terminal or source the file from an interactive shell:

. "$HOME/.bashrc"

java or javac Is Still Not Found

If JAVA_HOME is set but Java commands still fail, check whether $JAVA_HOME/bin appears in your shell path:

printf '%s\n' "$PATH" | tr ':' '\n' | grep -Fx "$JAVA_HOME/bin"
/usr/lib/jvm/default-java/bin

If the command returns nothing, reload the matching profile or ~/.bashrc file. If /usr/lib/jvm/default-java itself is missing, install default-jdk or choose a version-specific OpenJDK package first.

Maven or Gradle Says JAVA_HOME Is Not Defined Correctly

If Maven, Gradle, Jenkins, or another Java tool reports that JAVA_HOME is not defined correctly, compare the Java binary under JAVA_HOME with Ubuntu’s active java alternative:

readlink -f "$JAVA_HOME/bin/java"
readlink -f /usr/bin/java
"$JAVA_HOME/bin/javac" --version

When the paths agree, the first two lines should point to the same binary, and javac should report the same JDK branch:

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

If javac is missing, install default-jdk or a version-specific JDK package. A JRE-only installation can run Java programs but does not provide the compiler or development headers many build tools expect.

javac Reports invalid target release or jni.h Is Missing

The error javac: invalid target release: 17 usually means the active compiler is older than the release your project targets. The error jni.h: No such file or directory usually means the project cannot find JDK development headers. Check the compiler branch and header path before changing project files:

javac --version
echo "$JAVA_HOME"
find "$(readlink -f "$JAVA_HOME")/include" -name jni.h -print
javac 21.0.10
/usr/lib/jvm/java-21-openjdk-amd64
/usr/lib/jvm/java-21-openjdk-amd64/include/jni.h

If the compiler is older than the project target, install the required OpenJDK branch and use the dynamic update-alternatives method so JAVA_HOME, java, and javac resolve to the same JDK family.

JAVA_HOME Includes bin by Mistake

A common mistake is setting JAVA_HOME to the executable directory:

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

Fix the value so it points one level higher:

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

After editing the relevant profile, verify the Java binary through JAVA_HOME:

"$JAVA_HOME/bin/java" --version

default-jvm Is Missing on Ubuntu

If a tutorial or script expects /usr/lib/jvm/default-jvm, check whether it was written for another distribution. Ubuntu uses /usr/lib/jvm/default-java for the default JDK symlink, so update the path or use the dynamic /usr/bin/java method when you need the active update-alternatives choice.

Remove JAVA_HOME Configuration

To remove the system-wide configuration, delete the environment and profile files:

sudo rm -f /etc/environment.d/90-java-home.conf /etc/profile.d/java-home.sh

Remove the managed block from your user shell file if you used the ~/.bashrc method:

sed -i '/^# LinuxCapable JAVA_HOME start$/,/^# LinuxCapable JAVA_HOME end$/d' "$HOME/.bashrc"

Open a new terminal after removing shell configuration so the old environment variables and PATH entries disappear from your session.

If you installed Java only for this setup and no longer need it, remove the default JDK meta-package:

sudo apt remove default-jdk

APT may leave the OpenJDK packages marked as autoremovable after the meta-package is gone. Simulate cleanup first and read the package list before removing anything:

apt -s autoremove

If the simulation lists only Java packages and other packages you no longer need, run the real cleanup:

sudo apt autoremove

Verify that the meta-package is no longer installed:

dpkg -l default-jdk | grep '^ii' || echo "default-jdk is not installed"
default-jdk is not installed

Conclusion

JAVA_HOME now points Ubuntu’s Java build tools at the correct JDK root, while PATH exposes the matching command-line tools only in the scopes you configured. Keep /usr/lib/jvm/default-java for Ubuntu’s default JDK branch, or use the dynamic block when update-alternatives controls your version. From here, install Apache Maven on Ubuntu or install OpenJDK 25 on Ubuntu for a fuller Java workflow.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show more of our fresh Linux tutorials in Top Stories and From your sources when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee

2 thoughts on “How to Set Java Environment Path in Ubuntu 26.04, 24.04 and 22.04”

    • 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 currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<blockquote>quote</blockquote> quote block

Got a Question or Feedback?

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

Verify before posting: