How To Install Java On Ubuntu 20.04
The Java JDK (Java Development Kit) is a development environment used for developing Java Applications. The Java JDK is a collection of programming tools, such as ass JRE (Java Runtime Environment), Java (Loader for Java Application), Javac (Compiler), Jar (Archiver), etc.
JDK or JRE
Application developers who are new to Java development often confuse the Java Development Kit with the Java Runtime Environment. The JDK is a collection of tools for developing Java applications, whereas the JRE is for running Java applications.
OpenJDK or Oracle Java
OpenJDK is an open-source implementation of the Oracle Java SE platform edition. There is no technical difference between OpenJDK and Oracle JDK.
Install Java on Ubuntu 20.04
Install OpenJDK or Oracle Java as per your requirement.
You can have multiple versions of Java (Both OpenJDK and Oracle Java) on your system. But, you can have only one default version of Java.
Install OpenJDK
Installing OpenJDK on Ubuntu 20.04 is a pretty straight forward process.
First, update the repository index.
sudo apt update
Install OpenJDK JDK
JDK
### Default JDK ie JDK 11 ### sudo apt install -y default-jdk ### Java JDK 11 ### sudo apt install -y openjdk-11-jdk ### Java JDK 8 ### sudo apt install -y openjdk-8-jdk
JDK Headless
### Default JDK ie JDK 11 ### sudo apt install -y default-jdk-headless ### Java JDK 11 ### sudo apt install -y openjdk-11-jdk-headless ### Java JDK 8 ### sudo apt install -y openjdk-8-jdk-headless
Install OpenJDK JRE
JRE
### Default JRE ie JRE 11 ### sudo apt install -y default-jre ### Java JRE 11 ### sudo apt install -y openjdk-11-jre ### Java JRE 8 ### sudo apt install -y openjdk-8-jre
JRE Headless
### Default JRE ie JRE 11 ### sudo apt install -y default-jre-headless ### Java JRE 11 ### sudo apt install -y openjdk-11-jre-headless ### Java JRE 8 ### sudo apt install -y openjdk-8-jre-headless
Install Oracle Java
There is no separate JRE (Java Runtime Environment). Oracle JDK v9.x and above now includes JRE as well.
Download Oracle Java JDK
You can use either the command line or browser to download the JDK.
Go to the Oracle JDK page to download JDK packages using the browser. Download the .deb package for the easy installation.
Oracle Java JDK 14:
Oracle Java JDK 11 (LTS):
Oracle Java JDK 8:
If you still want to download through the command line, install the wget package.
sudo apt install -y wget
Then, use the wget command to download Oracle Java using the terminal.
### Oracle Java JDK 14 ### wget --no-check-certificate --no-cookies --header "Cookie: 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 ### Oracle Java JDK 11 ### LOGIN REQUIRED ### Oracle Java JDK 8 ### LOGIN REQUIRED
Install Oracle Java JDK
Install Oracle Java JDK using the apt command.
### Oracle Java JDK 14 ### sudo apt install -y ./jdk-14.0.1_linux-x64_bin.deb ### Oracle Java JDK 11 (LTS) ### sudo apt install -y ./jdk-11.0.7_linux-x64_bin.deb ### Oracle Java JDK 8 ### sudo tar -zxvf jdk-8u251-linux-x64.tar.gz -C /usr/lib/jvm/
By default,Oracle JDK is installed in /usr/lib/jvm directory when you use the .deb package.
Use the update-alternatives command to create a symbolic link for Java with Oracle JDK. Change the Java path depending on the JDK version.
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-14.0.1/bin/java 1
Set Default Java Version
Use the update-alternatives command to set the default java version.
sudo update-alternatives --config java
Select Java:
If your system has multiple Java versions, the above command will list all Java versions like below.
There are 3 choices for the alternative java (providing /usr/bin/java). 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-8-openjdk-amd64/jre/bin/java 1081 manual mode * 3 /usr/lib/jvm/jdk-14.0.1/bin/java 1 manual mode Press to keep the current choice[*], or type selection number: 2
Enter the number below selection column to set the default Java version.
Here, I chose 2 for OpenJDK 8.
Verify Java Version
Check the Java version using the following command.
java -version
Output: openjdk version "1.8.0_252" OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1ubuntu1-b09) OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
The above output may vary depending upon the version you chose it to be the default Java version.
Setup Environmental Variables
Java applications often require JAVA environment variables to be set in the system.
Create a file under /etc/profile.d directory to place the variables for all users.
sudo nano /etc/profile.d/java.sh
Change the variables path based on the Java location.
export PATH=$PATH:/usr/lib/jvm/java-11-openjdk-amd64/bin/
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/
export J2SDKDIR=/usr/lib/jvm/java-11-openjdk-amd64/
Load the environments into the current session.
source /etc/profile.d/java.sh
To set the environment variables for a particular user, place the above variables in ~/.bash_profile file.
Conclusion
I hope this post helped you install Java on Ubuntu 20.04. Please share your feedback in the comments section.