Atlantic.Net Blog

How to Install and Manage Java Versions on Oracle Linux 8

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. The Java package is offered 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 Oracle Linux 8.

Prerequisites

  • A server running Oracle Linux 8 on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Oracle Linux 8 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your server, run the following command to update your base system with the latest available packages.

dnf update -y

Install OpenJDK 18

At the time of writing this article, the latest version of Java OpenJDK is version 18. By default, the latest version is not available in the Oracle 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:

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 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-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 output:

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

At the time of 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 easily switch between multiple Java versions:

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

In this post, we 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!

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year