Installing OpenJDK on Ubuntu

View more Tutorials:

1- Which version should you install?

OpenJDK is an open source code of the Java ( Java Development Kit) maintained and developed by Oracle and the community. OpenJDK has similar features as Oracle JDK.
You may find them interesting:
Until 2020, Oracle has released  Java 13, but there are only a few long-term supported versions,  Java 8, 11, 13. Therefore, you can use one of these versions rather than other versions which are no longer supported by Oracle and there are very few users nowadays.
Java 8, despite being released in March 2014, still has a large number of users today, due to its long-term Oracle support, and its stability.

2- Installing OpenJDK 8

Installing OpenJDK 8 is extremely simple. On Ubuntu, open Terminal and execute some commands as follows:
sudo apt update
With OpenJDK version 8, 9:
# Install OpenJDK 8:

sudo apt install openjdk-8-jdk

# Install OpenJDK 9:

sudo apt install openjdk-9-jdk
Then check the result of the installation:
java -version

3- Installing OpenJDK 11

Installing OpenJDK 11 is a bit more complicated than OpenJDK 8 because you need to download and install it.
Download OpenJDK 11:
wget https://download.java.net/java/ga/jdk11/openjdk-11_linux-x64_bin.tar.gz
Next, extract the file that you have just downloaded in the previous step.
tar xzvf openjdk-11_linux-x64_bin.tar.gz
After that, move the folder you have got after extracting to  /usr/lib/jvm folder .
# Create directory:

sudo mkdir /usr/lib/jvm

# Move:

sudo mv jdk-11 /usr/lib/jvm/openjdk-11-manual-installation/
Finally, you need to make sure that the java & javac commands are pointing to the right location.
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/openjdk-11-manual-installation/bin/java 1

sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/openjdk-11-manual-installation/bin/javac 1

# keytool (Optional)

sudo update-alternatives --install /usr/bin/keytool keytool /usr/lib/jvm/openjdk-11-manual-installation/bin/keytool 1
Now check what you installed:
java -version

4- Installing OpenJDK 13

First of all, you need an address to download​​​​​​​ OpenJDK 13:
You will have an address to download OpenJDK similar below:

https://download.java.net/java/GA/jdk13.0.2/d4173c853231432d94f001e99d882ca7/8/GPL/openjdk-13.0.2_linux-x64_bin.tar.gz

Next, use the command to download it:
wget https://download.java.net/java/GA/jdk13.0.2/d4173c853231432d94f001e99d882ca7/8/GPL/openjdk-13.0.2_linux-x64_bin.tar.gz
 
After that, extract the file you have just downloaded in the previous step.
tar xzvf openjdk-13.0.2_linux-x64_bin.tar.gz
Then move the folder you have got after extracting to /usr/lib/jvm folder.
# Create directory:

sudo mkdir /usr/lib/jvm

# Move:

sudo mv jdk-13.0.2   /usr/lib/jvm/openjdk-13-manual-installation/
 
Finally, you need to make sure that the java & javac commands are pointing to the right location.
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/openjdk-13-manual-installation/bin/java 1

sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/openjdk-13-manual-installation/bin/javac 1

# keytool (Optional):

sudo update-alternatives --install /usr/bin/keytool keytool /usr/lib/jvm/openjdk-13-manual-installation/bin/keytool 1
Let's check the installation result:
java -version
 

5- Set Default Version

If you have multiple versions of JDK installed on your operating system, you need to specify a default version.
Check to see which versions of JDK are installed on your computer:
sudo update-alternatives --config java
You will get similar results as follows, enter 1 (or 2, ...) to select the default version you would like to use.
sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/openjdk-11-manual-installation/bin/java 1111 auto mode
1 /usr/lib/jvm/openjdk-11-manual-installation/bin/java 1111 manual mode
2 /usr/lib/jvm/openjdk-13-manual-installation/bin/java 1081 manual mode

Press to keep the current choice[*], or type selection number:
Finally, set the JAVA_HOME environment variable:
export JAVA_HOME=/usr/lib/jvm/openjdk-13-manual-installation/bin/java
 
Now check the result of setting the environment variable:
echo $JAVA_HOME

View more Tutorials: