Change default Java version in Ubuntu 20.04 LTS

We consider this article to be an extension to the previous article – Install OpenJDK JRE / JDK in Ubuntu 20.04 LTS. As you would notice, we have already installed version 11 of OpenJDK JRE / JDK in Ubuntu 20.04 LTS release. Now, consider a scenario wherein we have also installed version 8 of OpenJDK JRE. In that case, the default version would still be the one which we have installed earlier i.e. version 11.0.7. So, in this article we would discuss how to change the default Java version in Ubuntu 20.04 LTS.

Just for clarity, we would first install OpenJDK Development Kit v11.0.7 and OpenJDK Java Runtime Environment v1.0.8_252. Thereafter, we would issue the specific command to change the default version of Java.

Note: Following operations would require you to have superuser privileges. In case you don’t have one, then contact your System Administrator for assistance.

Install different versions of OpenJDK Development Kit and Java Runtime Environment

Since the packages are already available in standard Ubuntu repository. Therefore, first update the Ubuntu repository to make the latest version of the package available. Hence, issue the following in terminal –

sudo apt update

Now, to install OpenJDK Development Kit v11.0.7

sudo apt install openjdk-11-jdk

Next, to install OpenJDK Java Runtime Environment v1.8.0_252

sudo apt install openjdk-8-jre
Change default Java version in Ubuntu 20.04 LTS

First, verify for the Java version installed –

java -version

It would return the output –

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)

Although we already installed version 1.8.0_252, But, when we pass the java -version command then it doesn’t mention the said version. Because, as of now v11.0.7 is the default version. To change that, we need to issue command –

sudo update-alternatives --config java

It would show us all the Java versions installed. Enter the selection number for OpenJDK JRE v1.8.0_252. Again –

java -version

This time it would return with –

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)

In conclusion, we have discussed how to change the default version of Java in Ubuntu 20.04 LTS release.