How to Upgrade OpenJDK 17 to OpenJDK 21 on Rocky Linux 9.5: Step-by-Step Guide

Upgrade OpenJDK 17 to OpenJDK 21 on Rocky Linux with this detailed guide to ensure your Java environment stays updated with the latest features and security improvements. Follow these steps to complete the upgrade seamlessly, and watch this video for a visual walkthrough.

Why Upgrade OpenJDK 17 to OpenJDK 21 on Rocky Linux?

Staying up to date with the latest Java versions, like OpenJDK 21, brings improved performance, security patches, and new features. This guide will help developers and administrators perform the upgrade without complications.


How to Upgrade OpenJDK 17 to OpenJDK 21 on Rocky Linux

Step 1: Verify Your Current OpenJDK Version

Before starting, check which version of OpenJDK is installed on your system:

java -version  

This will confirm whether you are currently using OpenJDK 17.

Step 2: Update Your System and Enable EPEL Repository

Run the following commands to ensure your system is up to date and the EPEL repository is enabled:

sudo dnf install epel-release -y  
sudo dnf update -y  

Installing OpenJDK 21 on Rocky Linux

Run the following command to install OpenJDK 21:

sudo dnf install java-21-openjdk -y  

If you’re a developer, install the java-21-openjdk-devel package for compiling Java programs:

sudo dnf install java-21-openjdk-devel -y  

Configuring OpenJDK 21 as Default (Alternatives)

Step 3: Configure Alternatives for Java and Javac

Set OpenJDK 21 as the default Java version:

sudo alternatives --config java  

Select the option for OpenJDK 21. Repeat the process for javac:

sudo alternatives --config javac  

Step 4: Set JAVA_HOME and Update PATH

Add the JAVA_HOME environment variable to your .bashrc file:

echo "export JAVA_HOME=/usr/lib/jvm/java-21-openjdk" >> ~/.bashrc  
echo "export PATH=$JAVA_HOME/bin:$PATH" >> ~/.bashrc  
source ~/.bashrc  

Verifying OpenJDK 21 Installation

Run these commands to confirm the installation:

java -version  
javac -version  
echo $JAVA_HOME  

Test OpenJDK 21 with a Sample Program

Create a test file named Test.java:

public class Test {  
    public static void main(String[] args) {  
        System.out.println("Java 21 setup is working!");  
    }  
}  

Compile and run the program:

javac Test.java  
java Test  

You should see the output:
Java 21 setup is working!


FAQs

1. Why should I upgrade OpenJDK 17 to OpenJDK 21 on Rocky Linux?
Upgrading ensures access to new Java features, better performance, and improved security.

2. Is OpenJDK 21 compatible with all applications?
Some older applications may not support newer Java versions. Test in a staging environment first.

3. How do I switch back to OpenJDK 17 if needed?
Use the sudo alternatives command to switch between installed versions.

4. Do I need java-21-openjdk-devel for development?
Yes, the java-21-openjdk-devel package includes development tools like the Java compiler.

5. What’s the difference between java-21-openjdk and java-21-openjdk-devel?
The java-21-openjdk package provides the runtime environment, while the -devel package includes development tools.


Conclusion

Upgrading OpenJDK 17 to OpenJDK 21 on Rocky Linux ensures your system is secure and ready for modern Java development. Follow this guide and watch this video for additional clarity. Subscribe for more Linux tutorials!

See also:

List of monitoring tools 

Linux Blogs

AWS Cloud Blogs

Database Blogs

DevOps Blogs

Interview Questions & Answers

Docker Blogs

Google Cloud Blogs







Leave a Comment

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