Java is the most popular programming language used to build applications and systems. Additionally, you can use Java to create games, chatbots, enterprise applications, and more. Java can be run on all operating systems.

In order to develop Java applications, you will need to install an IDE on your system. It can be provided by OpenJDK or Oracle JDK. Java JDK is a free, open-source, and powerful software development environment used for developing Java Applications. JDK is a collection of programming tools including Javac, JRE, Jar, and Java.

In this post, we will show you how to install and manage different Java versions on Rocky Linux. This procedure is compatible with Rocky Linux 8 and Rocky Linux 9.

Install Java OpenJDK

In this section, we will show you how to install OpenJDK version 21, 11, and 8 on Rocky Linux 8.

Install OpenJDK 21

At the time of writing this article, the latest version of Java OpenJDK is version 21. By default, the latest version is not available in the Rocky Linux 8 default repository, so you will need to install it from the source.

First, download the latest version of OpenJDK from the Java official website:

wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz

Once the download is completed, install the tar package and extract the downloaded file with the following command:

dnf install tar -y
tar -xvf jdk-21_linux-x64_bin.tar.gz

Next, move the extracted directory to the /opt directory:

mv jdk-21.0.1 /opt/

Next, you will need to add the Java source path to the system environment. You can do it by creating the file java.sh:

nano /etc/profile.d/java.sh

Add the following lines:

export JAVA_HOME=/opt/jdk-21.0.1
export PATH=$PATH:$JAVA_HOME/bin

Save and close the file, then activate the Java path using the following command:

source /etc/profile.d/java.sh

Next, verify the Java path using the following command:

echo $JAVA_HOME

Sample output:

/opt/jdk-21.0.1

You can also verify the Java version using the following command:

java --version

You will get the following output:

java 21.0.1 2023-10-17 LTS
Java(TM) SE Runtime Environment (build 21.0.1+12-LTS-29)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-LTS-29, mixed mode, sharing)

Install OpenJDK 11 and OpenJDK 8

By default, OpenJDK 11 is available in the Rocky Linux default repository. You can install it using the following command:

dnf install java-11-openjdk-devel -y

You can also install the OpenJDK 8 using the following command:

dnf install java-1.8.0-openjdk-devel

Install Oracle JDK 17

At the time of writing this article, the latest version of Oracle JDK is 17. First, you will need to download it from its official website:

wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm

Once the download is completed, you can install it using the following command:

rpm -ivh jdk-17_linux-x64_bin.rpm

You will get the following output:

warning: jdk-17_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Verifying... ################################# [100%]
Preparing... ################################# [100%]
Updating / installing...
1:jdk-17-2000:17.0.1-ga ################################# [100%]

Next, verify the Java version using the following command:

java --version

Sample output:

java 17.0.1 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)

Manage Java Versions

If you are using multiple Java versions in your project, then you can easily switch between them using the following command:

alternatives --config java

You will be asked to set the default Java versions as shown below:

There are 3 programs which provide 'java'.

Selection Command
-----------------------------------------------
1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.el8_4.x86_64/bin/java)
2 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el8_4.x86_64/jre/bin/java)
*+ 3 /usr/java/jdk-17.0.1/bin/java

Enter to keep the current selection[+], or type selection number: 2

Select your preferred option and hit Enter to set the default Java version.

Next, verify your current Java version using the following command:

java -version

Sample output:

openjdk version "1.8.0_312"
OpenJDK Runtime Environment (build 1.8.0_312-b07)
OpenJDK 64-Bit Server VM (build 25.312-b07, mixed mode)

Conclusion

In the above guide, you learned how to install OpenJDK 21, 11, 8, and Oracle JDK 17 on Rocky Linux 8. You also learned how to manage and switch between different Java versions. You can now easily install and manage your preferred Java version on your system. Get started on your virtual private server from Atlantic.Net!