When you set the Java environment path in Ubuntu, you configure the JAVA_HOME variable that tells your system where Java is installed. Development tools like Maven, Gradle, Tomcat, and many IDEs rely on this environment variable to locate Java binaries and libraries. Without a properly configured Java environment path, these applications cannot find the Java compiler, runtime, or associated tools, causing build failures and runtime errors.
This guide shows how to install Java, locate your Java installation directory, and set the Java environment path permanently in Ubuntu. You will learn system-wide configuration methods using /etc/environment and user-specific alternatives using .bashrc, along with verification steps and troubleshooting tips for common issues.
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
This command checks the current Java version installed on your system, if any.
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
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. You can choose between system-wide configuration (affects all users) using /etc/environment or user-specific configuration (affects only your account) using .bashrc.
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
Changes to /etc/environment take effect only after you log out and log back in, or reboot the system. 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/
On modern Ubuntu releases that read the /etc/environment.d/ directory, 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.
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.
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
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 script queries update-alternatives for the current Java executable path, then strips the /bin/java suffix using Bash parameter substitution to get the JVM root directory. 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:
source ~/.bashrc
Verifying Java Environment Path Configuration
Validate your configuration by checking the JAVA_HOME and PATH variables:
echo $JAVA_HOME
echo $PATH
The first command should display your Java installation directory (for example, /usr/lib/jvm/java-21-openjdk-amd64). The second command includes $JAVA_HOME/bin if you added the PATH export. 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"
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 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
If the file is missing or incorrect, recreate it using the command from the system-wide method so $JAVA_HOME expands correctly.
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.
Conclusion
The JAVA_HOME environment variable tells build tools and IDEs where to find your Java installation. Ubuntu offers two configuration methods: system-wide setup via /etc/environment for servers and multi-user systems, or user-specific configuration via .bashrc for development workstations. After configuration, verify JAVA_HOME persists across new terminal sessions and that Java commands execute without specifying full paths. When you install new Java versions or switch between them, update JAVA_HOME to point to the correct installation directory.
Mageia doesn’t seem to have /etc/environment, but to use /etc/ profile or .bashrc instead.
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~/.bashrcand source it. Examples:Let me know if you would like a variant that auto-detects the active JVM.