How to Install OpenJDK 8 on Ubuntu 24.04, 22.04 or 20.04

OpenJDK 8 is an open-source implementation of the Java Platform, Standard Edition (SE), and is widely used for developing and running Java applications. Despite being a slightly older version, OpenJDK 8 remains popular due to its stability and compatibility with many enterprise and legacy systems. It includes essential features for running Java applications, making it a critical tool for developers and system administrators.

On Ubuntu 24.04, 22.04, or 20.04, you can easily install OpenJDK 8 using the command-line terminal via the Ubuntu default repository. This method is straightforward and ensures that you have a stable version of OpenJDK 8 with security updates provided by the Ubuntu repositories. This guide will walk you through the installation process using the command-line terminal.

Update Ubuntu System Packages Before OpenJDK 8 Installation

Before embarking on the installation of OpenJDK 8, it is paramount that your Ubuntu system is up-to-date. This is a recommended and vital step, as it helps avoid conflicts or potential errors during installation. Updating ensures that you have the latest security patches, bug fixes, and software improvements for the packages installed on your system.

To update your Ubuntu packages, execute the following command in the terminal:

sudo apt update && sudo apt upgrade

Once you’ve successfully executed this command, your Ubuntu system should be up-to-date and prepared for the OpenJDK 8 installation.

Verify Availability of OpenJDK 8 Packages

Verifying the availability of OpenJDK 8 packages is crucial before proceeding with the installation. Generally, the default Ubuntu repository is a reliable source for the OpenJDK installation. However, we need to confirm its presence first.

In your terminal, use the following command to find the availability of OpenJDK 8 packages:

apt-cache search openjdk-8 | grep openjdk-8

Let’s understand the components of this command:

  • apt-cache search openjdk-8: This queries the package database for all packages related to ‘openjdk-8’.
  • |: This is the pipe operator. It takes the output from the preceding command and sends it as input to the following command.
  • grep openjdk-8: This filters the output from the ‘apt-cache search’ command to only display lines that contain ‘openjdk-8’.

Install OpenJDK 8 JDK and JRE

With the availability of the OpenJDK 8 packages confirmed, you can now proceed to install them. The following terminal commands will install the Java Runtime Environment (JRE) and the Java Development Kit (JDK).

To install the OpenJDK 8 JRE, use:

sudo apt install openjdk-8-jre -y

For the OpenJDK 8 JDK, use:

sudo apt install openjdk-8-jdk -y

The -y flag in these commands automatically answers ‘yes’ to the prompts, facilitating a hassle-free installation.

Switch Between Different Java Versions on Ubuntu

Check the Current Java Version

Once OpenJDK 8 is successfully installed on your Ubuntu system, there might be a situation where the java –version command returns a version other than the one you just installed. This is likely because your system already has a newer version of Java installed, which takes precedence.

When running the command java –version, you should see a description of the Java version that’s currently active on your system. By default, your system’s most recent version of Java is set as the active version.

Here’s how to check the active version:

java --version

For example, if OpenJDK 17 is the latest version installed on your machine, it will be displayed as the active version, even if OpenJDK 8 was installed afterward.

List All Available JDK Versions

Let’s list all the Java versions installed on your system:

sudo update-alternatives --config java

This command will present you with a list of all Java versions installed on your Ubuntu system. Each version is assigned a priority number, with the version with the highest number set as the default.

Switch to OpenJDK 8 on Ubuntu

Now that you have a clear understanding of what versions of Java are available on your system, you can choose to switch to any version that fits your requirements. If you want to switch to OpenJDK 8, you can do so using the update-alternatives command again.

Test Java With a ‘Hello World’ Application

One of the fundamental steps post any software installation is to verify its successful implementation. With the installation and setup of OpenJDK 8 now complete, let’s create a simple ‘Hello World’ application. This will serve as a practical test and confirmation that Java is functioning as expected on your system.

Create Java File

Let’s start by creating a new Java program file. Open your terminal and use the following command:

nano hello.java

This command instructs the nano text editor to create a new file named ‘hello.java’.

Add Java Code

Inside the ‘hello.java’ file, let’s add a simple Java program. The program will output a message to your console when it’s run. Insert the following code into your ‘hello.java’ file:

public class hello {
  public static void main(String[] args) {
    System.out.println("hi from LinuxCapable!");
  }
}

To save your work in the nano editor, use the keyboard combination CTRL+O, followed by CTRL+X to exit.

Compile Java Code

With our Java program written, it’s time to compile the code. Compiling translates our source code into bytecode, which the Java Virtual Machine (JVM) can execute.

Run the following command to compile your Java program:

javac hello.java

If you encounter any issues with compiling, there could be a version mismatch. In that case, specify the release version with the –release flag. Here’s how to do it for OpenJDK 8:

javac --release 8 hello.java

Runn Java Code Test Hello Application

Once the code has been successfully compiled, you can run your Java program. Do this with the java command, followed by the class name you want to run (without the ‘.java’ extension).

Run your ‘Hello World’ program with the following command:

java hello

Example output:

Hi from LinuxCapable!

Managing OpenJDK 8

Remove OpenJDK 8 JDK and JRE

Run the following command in your terminal to remove both the OpenJDK 8 JRE and JDK, along with any related dependencies:

sudo apt remove openjdk-8-jre openjdk-8-jdk

Modify the command to suit your installed packages.

Conclusion

With OpenJDK 8 installed on your Ubuntu system via the default repository, you have access to a stable and reliable Java development environment. This method ensures that you receive regular security updates, keeping your Java setup secure and up-to-date. Whether you’re developing Java applications or running legacy systems, OpenJDK 8 provides the necessary tools and compatibility to meet your needs on Ubuntu.

Leave a Comment