How to Set Java Environment Path in Ubuntu

This guide will demonstrate how to use the command-line terminal to set the Java environment path in Ubuntu, such as for servers or desktop environments.

Setting the Java environment path in Ubuntu is crucial for developers looking to streamline their Java application development and execution process. This guide provides the necessary steps to configure your system’s Java environment path effectively. Through this setup, you’ll ensure that your Ubuntu system recognizes and locates Java executables seamlessly, facilitating a smoother development experience.

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.

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.

Checking the Java version in Ubuntu before configuring JAVA_HOME.
Screenshot showing how to check the Java version in Ubuntu.

Setting the Java Environment Path

Understand Ubuntu’s Environment Variables

Environment variables are key in Ubuntu for locating necessary files and settings. The JAVA_HOME variable indicates the Java installation directory.

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-java-alternatives command to list all Java installations:

update-alternatives --list java

This command will display all installed Java versions and their paths. Identify the path corresponding to the Java version you want to use. In our example, the output was “/usr/lib/jvm/java-21-openjdk-amd64“.

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.

Set Permanent JAVA_HOME

Below, we will cover two more options for permanently setting the environment.

Set JAVA_HOME via nano

Edit the environment file using nano:

sudo nano /etc/environment

In the nano editor, add:


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

Update the PATH Variable

Ensure the system recognizes Java commands by updating the PATH variable:

export PATH=$PATH:$JAVA_HOME/bin

This command appends the Java binary directory to the existing PATH.

Set JAVA_HOME via echo command

Instead of nano, you can use echo to set JAVA_HOME. For instance:

echo "JAVA_HOME=\"/usr/lib/jvm/java-x.x.x-openjdk\"" | sudo tee -a /etc/environment > /dev/null

This command directly writes to the /etc/environment file without opening an editor.

Verifying Java Environment Path Configuration

Validate your configuration by checking the JAVA_HOME and PATH variables:

echo $PATH

These commands should return your configured paths, indicating a successful setup.

Best Practices for Java Environment Setup

  • Regularly update Java to leverage the latest features and security enhancements.
  • Document changes to your Java environment for troubleshooting and maintenance.
  • Verify Java installations and configurations post-update to ensure continuity.
  • Utilize development tools like Maven and Gradle to standardize builds and dependencies.


Setting up the Java environment path in Ubuntu enhances your Java development efficiency. Following the steps outlined, ensure your system recognizes and executes Java applications correctly. Regular updates and adherence to best practices guarantee a robust and efficient Java development environment.

Relevant Links

Explore these resources for further information on Java and its environment on Ubuntu:

  • OpenJDK: Access the OpenJDK community and its projects.
  • OpenJDK Wiki: Find in-depth information and guides on OpenJDK.
  • OpenJDK GitHub: Explore OpenJDK’s source code and contribute to its development.

Leave a Comment