Java is an open-source, powerful, and widely used programming language. It is used to build web-based applications, games, chatbots, enterprise applications, and more. It is cross-platform and can be run on all operating systems.

You will require an IDE on your system to develop Java applications. OpenJDK or Oracle JDK offers the Java package. Java JDK is a free, open-source, and powerful software development environment for developing Java Applications. JDK is a collection of programming tools, including Javac, JRE, Jar, and Java.

This post will show you how to install and manage different Java versions on Oracle Linux 8.

Install OpenJDK 18

When writing this article, the latest version of Java OpenJDK is version 18. The newest version is unavailable in the Oracle Linux 8 default repository, so you must install it from the source.

First, update your Operating System, and then download the latest version of OpenJDK from the Java official website:

dnf update -y
curl  -O https://download.java.net/java/GA/jdk18/43f95e8614114aeaa8e8a5fcf20a682d/36/GPL/openjdk-18_linux-x64_bin.tar.gz

Once the download is completed, extract the downloaded file with the following command:

tar -xvf openjdk-18_linux-x64_bin.tar.gz

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

mv jdk-18 /opt/

Next, you must 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-18
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-18

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

java --version

You will get the following result:

openjdk 18 2022-03-22
OpenJDK Runtime Environment (build 18+36-2087)
OpenJDK 64-Bit Server VM (build 18+36-2087, mixed mode, sharing)

Install OpenJDK 11 and OpenJDK 8

By default, OpenJDK 11 is available in the Oracle 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 from the Oracle Linux default repository using the following command:

dnf install java-1.8.0-openjdk-devel

Install Oracle JDK 18

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

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

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

rpm -ivh jdk-18_linux-x64_bin.rpm

You will get the following output:

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

Next, verify the Java version using the following command:

java --version

Manage Java Versions

Java also allows you to install and manage multiple Java versions in the same system. You can use the “alternatives –config java” command to switch between various Java versions easily:

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.15.0.10-2.el8_6.x86_64/bin/java)
   2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.332.b09-2.el8_6.x86_64/jre/bin/java)
*+ 3           /usr/java/jdk-18.0.1.1/bin/java

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

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

Conclusion

This post explained how to install OpenJDK 18, 11, 8, and Oracle JDK 18 on Oracle Linux 8. You also learned how to manage and switch between different Java versions. You can now start developing a Java application using your preferred Java version. Try it on dedicated hosting from Atlantic.Net!