How To Set JAVA_HOME Environment Variable On Ubuntu 22.04 LTS

In this Java tutorial post, we will show you the step to set JAVA_HOME Environment variable on Ubuntu 22.04 LTS. At first make sure that you have Java installed in your Ubuntu 22.04 LTS. Even if there is no Java installed then we have solution for it too. JAVA is one of the most popular programming languages. Many programs are written using Java programming language and the program written on JAVA uses the JAVA_HOME environment variable to determine the Java installation location. It’s confusing but easy to set the JAVA_Home environment variable on Ubuntu 22.04 LTS. At first, you need to determine where JAVA is installed.

How To Install Java on Ubuntu 22.04 LTS

  • At first, We need to install JRE or Java runtime environment.
  • The default Ubuntu packages repositories contain the packages for the OpenJDK 11.
  • The default repository also contains OpenJDK 8 previous stable release packages.

1. Install Java 11 on Ubuntu 22.04 LTS

Run the following commands to install OpenJDK 11 package on Ubuntu 22.04 . Java 11 is the latest LTS version that is available for installation for Ubuntu 22.04 LTS

sudo apt update
sudo apt install openjdk-11-jdk

OpenJDK 11 has been installed.. Run the following command to  verify it by checking the Java version:

java -version

openjdk version "11.0.7" 2020-04-14
OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)

 

2. Install Java 8 on Ubuntu On Ubuntu 22.04 LTS.

Run the following command to install OpenJDK 8 package on Ubuntu 22.04 LTS.

sudo apt update
sudo apt install openjdk-8-jdk

3. Install Oracle Java 14 On Ubuntu 22.04 LTS.

Run the following command to download the Debian file of JAVA.

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn-pub/java/jdk/14.0.1+7/664493ef4a6946b186ff29eb326336a2/jdk-14.0.1_linux-x64_bin.deb"

How To Set JAVA_HOME Environment Variable On Ubuntu 22.04

Run the following update-alternatives command to find the path of JAVA installation in your Ubuntu 22.04 LTS.

sudo update-alternatives --config java

Output

Selection Path Priority Status
------------------------------------------------------------
0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
* 2 /usr/lib/jvm/java-11-oracle/bin/java 1091 manual mode

Press  to keep the current choice[*], or type selection number:

Now, we can find the location of the JAVA and its associate.

OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java and  Oracle Java is located at /usr/lib/jvm/java-11-oracle/jre/bin/java.

Open /etc/environment using nano or your favorite text editor:

sudo nano /etc/environment

Make sure that the path to the JAVA installation is correct and now you need to go to the end of the file, and add the following line.  Save the file and exit the editor.

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Now you need to reload this file to apply the changes to your current session:

source /etc/environment

Run the following command if you want to verify that the environment variable is set:

echo $JAVA_HOME

You will see the output like below:

Output
/usr/lib/jvm/java-11-openjdk-amd64

Now, you are done

Leave a comment

Your email address will not be published. Required fields are marked *