How to Install Java on Rocky Linux 9

Java is a powerful programming language that enables developers to create robust, high-performance applications. Java is fast, secure, and reliable, making it an ideal choice for developing mission-critical applications. Additionally, Java is platform-independent, meaning that compiled Java code can be run on any platform that supports Java. This makes Java an ideal choice for developing cross-platform applications. Lastly, Java is widely used in various industries, including financial services, healthcare, retail, and automotive. As a result, a large community of Java developers can share best practices and assist each other in resolving challenges. Consequently, organizations that use Java can benefit from the vast ecosystem of tools and resources that are available to them.

The following tutorial will show you how to install Java on Rocky Linux 9 using the standard developer repository and various versions if required using the command line terminal.

Update Rocky Linux

First, update your system to ensure all existing packages are up to date. This will ensure no conflicts arise as best as possible during the installation.

sudo dnf upgrade --refresh

Install Java (OpenJDK)

The first thing you will need to do is enable the CRB directory using the following command.

sudo dnf config-manager --set-enabled crb

Next, list the versions of Java available on your system using the following command.

dnf search openjdk

If you are after a particular version, I suggest using grep to folder down the results. For example, finding the Java 17 LTS version would look like this.

dnf search openjdk | grep java-17

Next, you have various options to install Java on your system. Below will focus on the main versions.

Install Java 17


sudo dnf install java-17-openjdk


sudo dnf install java-17-openjdk-headless

Devel (Development):

sudo dnf install java-17-openjdk-devel

Install Java 11 LTS


sudo dnf install java-11-openjdk


sudo dnf install java-11-openjdk-headless

Devel (Development):

sudo dnf install java-11-openjdk-devel

Install Java 8 LTS


sudo dnf install java-1.8.0-openjdk


sudo dnf install java-1.8.0-openjdk-headless

Devel (Development):

sudo dnf install java-1.8.0-openjdk-devel

Test Java – Create Hello World Application

To finish off, testing installations of these kinds are always handy to confirm everything is working correctly after installation. The easy way is to create a small program script to try using the famous Hello World example.

First, create the Java program file as follows

sudo nano

Next, add the following Java code to the file:

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

Save the file (CTRL+O), then exit (CTRL+X).

Finally, run the Java code with the following command.

java hello

Example output:

[ ~]$ java
Hello from LinuxCapable!

Congratulations, everything is working correctly.

Switching Default Versions of Java

Typically, most systems will start to have multiple installations and versions of JDK or OpenJDK, especially as a developer. Sometimes, you may want one set as default but sometimes need to switch them around. This can be easily achieved using the following command.

sudo update-alternatives --config java

Example output:

How to Install Java on Rocky Linux 9

The above command will print a list of the default version and all other versions in a numbered list.

In the terminal, you will see the message to choose which number to set as default. Enter the number, and press the ENTER KEY; the example will change Java 11 LTS, the current default above with the Asterix *, to Java 17 LTS by entering selection number 1.

You can confirm this by checking the version command on your system.

java --version

Example output:

[ ~]$ java --version
openjdk 2022-08-12 LTS
OpenJDK Runtime Environment (Red_Hat- (build
OpenJDK 64-Bit Server VM (Red_Hat- (build, mixed mode, sharing)

As the default version output is now Java 17, you have successfully changed the default version. For switching back to another version of Java, use the same principle.

Comments and Conclusion

Java has become one of the most popular programming languages because it is versatile and helpful in developing applications on many devices. Java is also fast, secure, and reliable, making it advantageous for users to incorporate into their programming.

Not what you were looking for? Try searching for additional tutorials.

Leave a Comment