Note:- These instructions will work to install OpenJDK not Oracle JDK.

Before you proceed, run the system Apt update command.

sudo apt update

Install OpenJDK 11 On Ubuntu

sudo apt install default-jre

The above command will install following packages...

openjdk-11-jre, openjdk-11-jre-headless

Java is installed now. Let us check java version...

java --version
openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu118.04.1, mixed mode, sharing)

Install JDK 11 On Ubuntu

To compile some specific JAVA based software, we need to install Java Development Kit JDK too.

sudo apt install default-jdk

Run following command to verify the installation...

javac --version
javac 11.0.8

Install OpenJDK 8 On Ubuntu

To install OpenJDK 8, run following command...

sudo apt install openjdk-8-jre-headless

Install JDK 8 On Ubuntu

sudo apt install openjdk-8-jdk-headless

Manage Multiple Versions Of Java On Ubuntu

Let us check the versions of Java we have...

sudo update-alternatives --config java

The output of above command is shown below. You will see a prompt to choose the version of Java. * 0 in the first "Selection" column means Java 11 is the default version.

There are 2 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

Press <enter> to keep the current choice[*], or type selection number: 2

Let us choose option 2 to set it to Java 8.

Now run java -version command again.

java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-0ubuntu2~18.04-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)

As we see above, the Java version changed to 8.

Similarly we can change the version of JDK also..

 sudo update-alternatives --config javac
There are 2 choices for the alternative javac (providing /usr/bin/javac).

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