Google Cloud

How to Install Java on Ubuntu 20.04

How to Install Java on Ubuntu 20.04. Java is one of the most popular programming languages which is widely used to built applications. It runs on all major operating systems and devices.

In this guide you are going to learn how to install Java Runtime Environment (JRE), Java Development Kit (JDK) and Open JDK.

Choose Best Hosting for your Business

PlatformReviewsPricing
Siteground★★★★★$3.95
Kinsta – Google Cloud★★★★★$30

Available Java versions in Ubuntu 20.04

By default Ubuntu 20.04 repositories include two OpenJDK packages, Java Runtime Environment (JRE) and Java Development Kit (JDK).

If you are not sure what to install you can choose the default OpenJDK (JDK 11) version.

Start by updating the packages and install Java OpenJDK 11.

sudo apt update
sudo apt upgrade

Install Java OpenJDK 11

At the time of writing Java 11 is the latest long term supported version available. This is the default version on Ubuntu 20.04.

Install Java 11 using the following commands.

 sudo apt install openjdk-11-jdk 

Once the installation is completed you can verify the Java version using the following command.

 java -version 

You will receive an output similar to the one below.

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) 

Now you have installed Java 11 on your Ubuntu 20.04.

Install Java OpenJDK 8

If your application is built using Java 8, you can install it using the following commands.

 sudo apt install openjdk-8-jdk 

Once the installation is completed you can verify the Java version using the following command.

 java -version 

You will receive an output similar to the one below.

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)

Now you have installed Java 8 on your Ubuntu 20.04.

Switch between Java Versions

If you have multiple Java versions installed, you can change the version easily using the following command.

 sudo update-alternatives --config java 

This command prompts you to choose Java 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  to keep the current choice[*], or type selection number: 

Choose the number and press Enter.

Configure JAVA_HOME Environment Variable

To setup path you need to find the installation path using the update-alternatives command.

 sudo update-alternatives --config java 

Here you will see the path of Java.

  • OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java
  • OpenJDK 8 is located at /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

Now you can add this to the environment file.

 sudo nano /etc/environment

Now add the path as shown below, here shown the method to add Java 11 to the path.

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

For the changes to be applied either logout and login or execute the following command for the current session.

source /etc/environment 

To verify the environment variable of Java

echo $JAVA_HOME

You will get the installation path you just set.

/usr/lib/jvm/java-11-openjdk-amd64

That’s all.

Conclusion

Now you have learned how to install Java and configure JAVA_HOME on Ubuntu 20.04.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.