How To Install OpenJDK 11 on Ubuntu 18.04

Introduction

In this tutorial we learn how to install OpenJDK 11 on Ubuntu 18.04 from Ubuntu repository. Java 11 is the current LTS (Long Term Support) version of Java.

Update Apt Package Index

First of all update apt package index using the command below.

sudo apt-get update

or we can also use apt command below.

sudo apt update

Searching OpenJDK Packages on Ubuntu Repository

To get list of available openjdk packages we can use command below.

sudo apt search ^openjdk-11

We will get output similar to below.

To search OpenJDK packages on Ubuntu we can also use apt-cache command below.

sudo apt-cache search ^openjdk-11

The output will be a little bit different that what we got from apt search command.

You can see sample output of this search below.

Install OpenJDK 11 on Ubuntu 18.04

In this section we learn how to install OpenJDK 11 package on Ubuntu 18.04. We will learn how to install multiple alternatives of Java 11 on Ubuntu 18.04.

Install OpenJDK 11 Java Development Kit (JDK)

To install OpenJDK 11 JDK package we can use the command below. For this tutorial I will install this package.

There are alternative packages that you can install we will learn about each method below.

sudo apt-get install openjdk-11-jdk
install openjdk 11 ubuntu 18.04 - apt-get install openjdk-11-jdk

To install OpenJDK 11 JDK headless package we can use the coomand below. You can install JDK headless if you need to develop or compile application that doesn’t have GUI like webapps

sudo apt-get install openjdk-11-jdk-headless
Install openjdk 11 ubuntu 18.04 - apt-get install openjdk-11-jdk-headless

Install OpenJDK 11 Java Runtime Environment (JRE)

To install OpenJDK 11 JRE package we can use the command below. You can install JRE if you need to run java application on desktop that using GUI.

sudo apt-get install openjdk-11-jre
Install openjdk 11 ubuntu 18.04 - apt-get install openjdk-11-jre

To install OpenJDK 11 JRE Headless package we can use the command below. You can install JRE headless to run Java application on server.

sudo apt-get install openjdk-11-jre-headless
Install openjdk 11 ubuntu 18.04 - apt-get install openjdk-11-jre-headless

Compile and Run Java 11 Application

In this section we will create a simple Java application, compile and run using openJDK javac and java application.

First, create new file named HelloHowtoDojo.java with contents below

class HelloHowtoDojo {
  public static void main(String[] args) {
      String version = System.getProperty("java.version");
      System.out.println("Hello howtodojo|");
      System.out.println("Java Version :"  + version);
  }
}

Second, compile the source code using command below.

javac HelloHowtoDojo.java

Third, run the application.

$ java HelloHowtoDojo
Hello howtodojo|
Java Version :11.0.7

Summary

In this tutorial we learn how to install Java OpenJDK 11 on Ubuntu 18.04. We learn to install JDK and JRE packages both headless and non-headless.

At the end we learn how to create, compile, and run Java application using Java 11 on Ubuntu 18.04.

Related Tutorials

References

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.