Install Java JDK on Ubuntu 18.04

Profile picture for user devraj

You may be confused which JDK to install Oracle or Open JDK? Well, Both OpenJDK and Oracle JDK are created and maintained currently by Oracle only.

OpenJDK and Oracle JDK are implementations of the same Java specification passed the TCK (Java Technology Certification Kit).

The Oracle JDK is targeted more towards an enterprise audience which values stability. It's based on one of the OpenJDK releases but is then given long term support (LTS)

There can be several ways of installing Oracle JDK.

Manually Download & Install Oracle JDK

Step 1: Navigate to Download Page of Oracle JDK and click on Below Image:

Step 2: Accept the agreement by selecting the radio button and click on ...tar.gz download link.

Step 3: Type below command to install JDK to /opt folder or any other directory you want

$ sudo tar xvzf jdk-12.0.2_linux-x64_bin.tar.gz -C /opt

Step 4: Verify JDK in /opt directory

$ ls -lh /opt

Step 5: The following linux commands will set Oracle JDK as system wide default. Amend the below commands to suit your installed version:

$ sudo update-alternatives --install /usr/bin/java java /opt/jdk-12.0.2/bin/java 1
$ sudo update-alternatives --install /usr/bin/javac javac /opt/jdk-12.0.2/bin/javac 1

Step 6: Check Java successfully installed using this commad

$java -version

It will return following if you have done anything wrong it will throw error.

java version "12.0.2" 2019-07-16
Java(TM) SE Runtime Environment (build 12.0.2+10)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing)