How to set Java environment path in Ubuntu
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
On Ubuntu, Java tools such as Maven, Gradle, and application servers often depend on a correct JAVA_HOME value and a working PATH. If those variables point to the wrong location, builds fail or use the wrong JDK version without obvious errors. A reliable setup includes discovery, configuration, and verification steps.
Find Installed JDK Paths
Start by confirming Java is installed and listing available JDK directories. Ubuntu commonly stores JDK installations under /usr/lib/jvm.
If Java is missing, install a JDK first:
After installation, check the path of the active java binary:
The output usually points to a path such as /usr/lib/jvm/java-17-openjdk-amd64/bin/java. The parent directory ending in /bin is useful for PATH; the directory one level above is your JAVA_HOME.
Set JAVA_HOME for One User Session
For per user shell configuration, add exports to ~/.bashrc or ~/.zshrc depending on your shell. This is the safest first step because it does not change settings for all users.
Verify:
If the printed path and versions match your target JDK, shell setup is working.
Configure System Wide Java Environment
For server environments, use a profile script in /etc/profile.d so all interactive users load the same defaults.
Open a new session and validate again with echo "$JAVA_HOME" and java -version.
For non interactive services managed by systemd, put environment values in the service unit or an environment file instead of relying on shell startup files.
Manage Multiple JDKs with update-alternatives
Ubuntu can install several JDK versions. Use update-alternatives to choose default binaries for commands like java and javac.
These commands show numbered choices. Select the desired path, then recheck:
Even when alternatives are set, keep JAVA_HOME aligned with the same JDK to avoid mixed runtime and compiler versions.
Example Verification Script
A short validation script is useful for onboarding and CI environment checks.
Save as check-java-env.sh, run with bash check-java-env.sh, and keep it in your repository documentation.
Troubleshoot Version Mismatch in Build Tools
Sometimes java -version is correct, but Gradle or Maven still reports another JDK. This usually means one tool has a local override. Check Gradle and Maven output, then inspect environment variables inside the same shell where you run the build.
If a CI runner still uses the wrong Java version, print these values at the start of your job and compare with local output. Keep the verification logs in pull requests when changing JDK versions so reviewers can confirm the migration is complete.
Common Pitfalls
- Setting
JAVA_HOMEto the binary path ending withjava. It should point to the JDK home directory, not the executable file. - Updating
.bashrcand expecting service processes to inherit it. Services usually need explicit environment settings. - Mixing JDK versions between
javaandjavac. Verify both commands after every change. - Forgetting to open a new shell after editing profile files. Old sessions keep old environment values.
- Using relative paths in exports. Prefer absolute paths under
/usr/lib/jvmfor consistency.
Summary
- Install and verify JDK packages before setting environment variables.
- Set
JAVA_HOMEand prependJAVA_HOME/bintoPATHin the correct shell file. - Use
/etc/profile.dfor shared server defaults. - Use
update-alternativeswhen multiple JDK versions are installed. - Validate with command outputs so tooling and builds use the intended Java version.

