How to set JAVA_HOME in Linux for all users
To define JAVA_HOME for all users, tweak /etc/environment:
To activate changes:
Or, set via /etc/profile.d creating a java_home.sh script:
Check with:
Locating the Java installation directory
Java's installation may differ. Locate it this way:
Reflect the correct JAVA_HOME in previous steps.
Instantly updating JAVA_HOME
To apply modifications without relogging:
For dynamically setting JAVA_HOME, add this to java_home.sh:
This keeps JAVA_HOME updated even post Java-upgrade.
Handling multiple versions of Java
If dealing with multiple Java incarnations, use update-alternatives managing symlinks to versions, pairing it with setting JAVA_HOME to:
Ensuring script's permissions
Scripts in /etc/profile.d/ require executable permissions:
This ensures all users get Java environments.
Sync across profiles for uniform experience
JAVA_HOME has to be identical in .bashrc and /etc/profile for seamless user experience. To tweak .bashrc:
This caters to interactive shells and user-specific bases.
Managing Java and Maven together
An incorrect JAVA_HOME can fire up Maven build errors. Check your Maven pointing to JAVA_HOME:
It should resonate with Java path in JAVA_HOME.
Was this article helpful?