How to Set JAVA_HOME Environment Path in Ubuntu
- Checking Your Java Installation
- Finding the Java Installation Path
- Setting the JAVA_HOME Variable
- Verifying the Configuration
- Conclusion
- FAQ

Setting the JAVA_HOME environment variable is crucial for Java developers and users on Ubuntu. This variable points to the directory where Java is installed, allowing applications to locate the Java runtime environment (JRE) and Java Development Kit (JDK). Whether you’re compiling Java programs or running Java-based applications, having the JAVA_HOME variable properly configured can save you from a lot of headaches.
In this tutorial, we will guide you through the steps to set the JAVA_HOME environment path in Ubuntu effectively. By the end of this article, you’ll have a clear understanding of how to configure your system for seamless Java development.
Checking Your Java Installation
Before setting the JAVA_HOME path, it’s essential to verify that Java is installed on your Ubuntu system. You can do this by running a simple command in your terminal.
java -version
Running this command will display the version of Java installed on your machine. If Java is installed, you will see a version number. If it’s not installed, you’ll need to install it first. You can use the following command to install OpenJDK:
sudo apt update
sudo apt install openjdk-11-jdk
After installation, you can again run the java -version
command to confirm that Java is correctly installed.
Output:
openjdk version "11.0.x" 202x-xx-xx
This output indicates that OpenJDK 11 is installed on your system. Once you have confirmed that Java is installed, you can proceed to set the JAVA_HOME variable.
Finding the Java Installation Path
To set the JAVA_HOME environment variable, you first need to locate the directory where Java is installed. Typically, for OpenJDK, this path is /usr/lib/jvm/java-11-openjdk-amd64
. However, you can find the exact path by running the following command:
readlink -f $(which java)
This command will return the path of the Java executable. You may need to navigate up a few directories to find the JAVA_HOME location. For example, if the output is something like /usr/bin/java
, you can find the installation path by following the symlink:
ls -l /usr/bin/java
Output:
lrwxrwxrwx 1 root root 22 Jan 1 00:00 /usr/bin/java -> /etc/alternatives/java
If you check the alternatives directory, you will see the actual installation path.
Output:
ls -l /etc/alternatives/java
This will lead you to the actual Java installation directory, which you will need for the next step.
Setting the JAVA_HOME Variable
Now that you have the Java installation path, you can set the JAVA_HOME environment variable. This can be done by editing the .bashrc
file or the .profile
file in your home directory. Here’s how to do it using the .bashrc
file:
- Open the
.bashrc
file in your favorite text editor. For example:
nano ~/.bashrc
- Add the following lines at the end of the file:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
export PATH=$JAVA_HOME/bin:$PATH
Make sure to replace /usr/lib/jvm/java-11-openjdk-amd64
with the actual path you found earlier.
-
Save the file and exit the text editor. If you are using nano, you can do this by pressing Ctrl+X, then
Y
, and finally Enter. -
To apply the changes, run the following command:
source ~/.bashrc
This command reloads the .bashrc
file, applying the new settings. You can verify that the JAVA_HOME variable is set correctly by running:
echo $JAVA_HOME
Output:
/usr/lib/jvm/java-11-openjdk-amd64
If you see the correct path in the output, congratulations! You have successfully set the JAVA_HOME environment variable.
Verifying the Configuration
After setting the JAVA_HOME variable, it’s essential to verify that everything is configured correctly. You can do this by checking both the JAVA_HOME variable and the Java version.
First, check the JAVA_HOME variable:
echo $JAVA_HOME
Output:
/usr/lib/jvm/java-11-openjdk-amd64
Next, check the Java version to ensure it corresponds to the JAVA_HOME path:
java -version
Output:
openjdk version "11.0.x" 202x-xx-xx
If both commands return the expected results, you have successfully set up your JAVA_HOME environment variable.
Conclusion
Setting the JAVA_HOME environment path in Ubuntu is a straightforward process that can significantly enhance your Java development experience. By confirming your Java installation, finding the installation path, and configuring the environment variable, you ensure that your system is ready for Java applications. This setup not only helps in compiling Java programs but also aids in running various Java-based applications seamlessly. Now that you have the knowledge, you can confidently set up your development environment and focus on what you do best: writing code.
FAQ
-
How do I check if Java is installed on my Ubuntu system?
You can check if Java is installed by running the commandjava -version
in the terminal. -
What should I do if Java is not installed?
You can install Java using the commandsudo apt install openjdk-11-jdk
. -
Where can I find the JAVA_HOME installation path?
You can find the installation path by runningreadlink -f $(which java)
and navigating up the directory tree. -
Can I set JAVA_HOME for other Java versions?
Yes, you can set JAVA_HOME for any installed Java version by updating the path accordingly. -
How do I make the JAVA_HOME variable permanent?
You can make it permanent by adding the export command to your.bashrc
or.profile
file.
I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.
LinkedIn