Introduction
Configuring the JAVA_HOME environment variable is a critical step for Java developers and applications running on Ubuntu. This variable points to the Java Development Kit (JDK) installation directory, allowing the operating system to locate JDK executables. Here is a complete guide on how to properly setup JAVA_HOME on Ubuntu.
Step 1: Install Java
Before setting JAVA_HOME, you must have Java installed. You can install the default JDK package available in the Ubuntu repository or a specific version of Oracle JDK or OpenJDK.
Install a specific version of JDK: For a specific version of OpenJDK, use:
sudo apt install openjdk-11-jdk
sudo apt install default-jdk
Replace 11 with the version number of your choice.
Install default JDK: Open a terminal and run the following command:
sudo apt update
Step 2: Find your Java installation location
To set JAVA_HOME, you need to know where Java is installed. Use the update-alternatives command to list all Java installations and their paths.
update-alternatives --list java
This command will show paths like /usr/lib/jvm/java-11-openjdk-amd64/bin/java. The directory up to java-11-openjdk-amd64 is the one you will use for JAVA_HOME.
Step 3: Set the JAVA_HOME environment variable
You can set JAVA_HOME temporarily or permanently.
Temporarily:
For a temporary setting, valid only in the current terminal session, use:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Replace /usr/lib/jvm/java-11-openjdk-amd64 with the path found in the previous step.
Permanently:
To set JAVA_HOME permanently, add it to your ~/.profile
, ~/.bashrc
, or /etc/environment
file. For most users editing ~/.bashrc
is sufficient.
Save and close the file.
Apply the changes by running:
source ~/.bashrc
Add the following line to the end of the file:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Open ~/.bashrc in a text editor:
nano ~/.bashrc
Step 4: Verify your configuration
To make sure JAVA_HOME is set correctly, open a terminal and run:
echo $JAVA_HOME
This command should display the path to your Java installation. Also, you can check if Java is working properly by running:
java -version
This should display the installed Java version, indicating that the configuration is correct.
Conclusion
Correctly setting the JAVA_HOME environment variable is essential for Java development and runtime on Ubuntu. Allows the system and Java applications to correctly locate the Java installation directory. By following the steps above, you can configure JAVA_HOME temporarily or permanently according to your needs. Always make sure to check your configuration to avoid any runtime issues with Java applications.