Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

It's finally here:

>> The Road to Membership and Baeldung Pro.

Going into ads, no-ads reading, and bit about how Baeldung works if you're curious :)

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

Keeping Java updated ensures both security and performance, making it essential to manage Java versions efficiently.

In this tutorial, we’ll walk through the process of identifying outdated Java versions. We’ll also see how to remove them and install new ones on a Linux system to stay on the latest and most secure release.

2. Checking Installed Java Versions

Before making any changes, we need to check which version of Java is currently installed on our system. Let’s open a terminal and run:

$ java -version

Let’s see an example output:

openjdk version "11.0.17" 2023-10-17
OpenJDK Runtime Environment (build 11.0.17+8-Ubuntu-222.04)
OpenJDK 64-Bit Server VM (build 11.0.17+8-Ubuntu-222.04, mixed mode)

The output shows that Java 11 is currently active. However, if multiple Java versions are installed, we need to determine which one is set as the default. To list all available Java alternatives, let’s run:

$ sudo update-alternatives --config java

Let’s see an example of what the output might look like:

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-17-openjdk-amd64/bin/java   1112      manual mode  

In this output, we can see that Java 11 is currently the default version because it is marked with an asterisk (*). Additionally, we notice that Java 17 is available but isn’t set as the default. The auto mode indicates that the system automatically selects the Java version based on priority.

3. Removing Old Versions of Java

Outdated Java versions can pose security risks and take up unnecessary space on our system. Let’s walk through the process of identifying and removing them.

3.1. Identifying Installed Packages

Let’s identify the installed Java package name using a package manager, such as apt on Debian-based systems:

$ dpkg --list | grep -i jdk

On Red Hat-based systems, let’s use:

$ rpm -qa | grep -i jdk

Let’s see an example output for a Debian-based system:

ii  openjdk-11-jdk-headless     11.0.17+8-1ubuntu2~22.04   amd64  OpenJDK Development Kit (JDK)  
ii  openjdk-17-jdk-headless     17.0.8+7-1ubuntu2~22.04    amd64  OpenJDK Development Kit (JDK)  

The output shows that both Java 11 and Java 17 are installed. Let’s take note of these package names for the next step.

3.2. Uninstalling Old Versions

Let’s replace <java-package-name> with the exact package name listed earlier to uninstall it. For Debian-based systems, let’s use:

$ sudo apt remove --purge <java-package-name>

For Red Hat-based systems, let’s run:

$ sudo yum remove <java-package-name>

After removing the package, let’s clean up any residual files to free up space:

$ sudo apt autoremove

Or, for Red Hat-based systems:

$ sudo yum autoremove

By doing this, we ensure the proper cleanup of all associated files, leaving our system neat and efficient.

4. Installing the Newest Version of Java

Installing the latest version of Java ensures that our system benefits from the latest features and security updates. The installation process depends on the package source we choose: official repositories or manual installation from Oracle.

4.1. Installing OpenJDK from Official Repositories

Most Linux distributions provide OpenJDK, the open-source implementation of Java, in their repositories. Currently, OpenJDK 8, OpenJDK 11, OpenJDK 17, and OpenJDK 21 are available in most Linux repositories.

Let’s install OpenJDK 21, the latest version, using our package manager.

For Debian-based systems, we can run:

$ sudo apt update
$ sudo apt install openjdk-21-jdk

For Red Hat-based systems, let’s use:

$ sudo yum install java-21-openjdk

These commands allow us to install OpenJDK 21 easily on both Debian and Red Hat-based systems.

4.2. Installing Oracle JDK Manually

For users who prefer the official Oracle JDK, let’s download the latest version from Oracle’s official site. Then, let’ extract the archive to the /opt/ directory:

$ tar -xvf jdk-21_linux-x64_bin.tar.gz
$ sudo mv jdk-21* /opt/

Next, let’s configure the system to recognize this new Java installation:

$ sudo update-alternatives --install /usr/bin/java java /opt/jdk-21*/bin/java 1
$ sudo update-alternatives --config java

Finally, let’s verify the installation by checking the Java version:

$ java -version

After completing these steps, we can confirm that the new JDK is successfully installed and configured.

5. Setting JAVA_HOME Environment Variable

After installing Java using Oracle JDK, it is essential to set the JAVA_HOME environment variable for proper system integration.

Let’s open the profile configuration file:

$ nano ~/.bashrc

Next, let’s add the following lines, but let’s make sure to replace /opt/jdk-21 with the path for the installed Java version if it differs from the one mentioned in this article:

export JAVA_HOME=/opt/jdk-21*
export PATH=$JAVA_HOME/bin:$PATH

To apply the changes, let’s run:

$ source ~/.bashrc

Finally, let’s confirm that the variable has been set correctly by running:

$ echo $JAVA_HOME

Once these steps are completed, the JAVA_HOME environment variable will be successfully configured.

6. Testing the New Java Installation

Let’s create a simple test program to verify that the Java installation is functioning correctly.

Firstly, let’s create a new HelloWorld.java file:

$ nano HelloWorld.java

Then, we add the given code to the file:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Next, let’s compile and run the program:

$ javac HelloWorld.java
$ java HelloWorld
Hello, World!

If the output is Hello, World!, it indicates that our Java installation is working as expected.

7. Conclusion

In this tutorial, we’ve walked through the steps to manage Java versions on Linux by removing outdated versions, installing the latest updates, and properly configuring the system. By following this guide, we ensure that our applications run securely and efficiently with the most recent Java runtime.