Install Java 17 on Ubuntu 20.04

Java is a programming language which is used in many systems and applications. OpenJDK and Oracle JDK are two implementations of the Java. These Java implementations is based on same specification. A main difference is licensing. OpenJDK is an open-source implementation of the Java. A commercial license is required for Oracle JDK.

The Java Runtime Environment (JRE) is a package which consists of Java Virtual Machine (JVM) and allows to run Java applications. The Java Development Kit (JDK) is full-featured package that includes JRE, debugging and development tools which allows to build and run Java applications.

Java 17 is a long-term support (LTS) release. This tutorial shows how to install Java JDK 17 (OpenJDK) on Ubuntu 20.04.

Install Java

Execute the following command to add the OpenJDK repository:

sudo add-apt-repository -y ppa:openjdk-r/ppa

Install Java 17:

sudo apt install -y openjdk-17-jdk

Once installed, we can check Java version:

java -version

Testing Java

Create a Main.java file:

nano Main.java

When a file is opened, add the following code:

public class Main
{
    public static void main(String[] args)
    {
        System.out.println("Hello world");
    }
}

Run the java command to test a program:

java Main.java

Uninstall Java

If you decided to completely remove Java and related dependencies, execute the following command:

sudo apt purge --autoremove -y openjdk-17-jdk

Remove GPG key and repository:

sudo rm -rf /etc/apt/trusted.gpg.d/openjdk-r_ubuntu_ppa.gpg
sudo rm -rf /etc/apt/sources.list.d/openjdk-r-ubuntu-ppa-focal.list

Leave a Comment

Your email address will not be published. Required fields are marked *