Java is a free, open-source, multipurpose, and high-level object-oriented programming language. Generally, it is used for creating web, mobile, and cloud applications. You can create games, chatbots, and other applications using the Java language. Java JDK is a collection of several tools including, JRE, Java, Javac, and Jar. There are two variants of JDK, OpenJDK and Oracle JDK.
OpenJDK is a completely open-source Java with a GNU General Public License while Oracle JDK requires a commercial license under the Oracle Binary Code License Agreement.
In this post, we will show you how to install OpenJDK 16 and Oracle JDK 16 on Rocky Linux 8.
Prerequisites
- A server running RockyLinux 8.
- A root password is configured on the server.
Update the System
First, it is recommended to update your system to the latest version of packages. You can update it using the following command:
dnf update -y
After updating all the packages, install other required packages with the following command:
dnf install wget curl -y
Once you are done, you can proceed to the next step.
Install OpenJDK 16
First, download the OpenJDK version 16 from its official website using the following command:
curl -O https://download.java.net/java/GA/jdk16.0.2/d4a915d82b4c4fbb9bde534da945d746/7/GPL/openjdk-16.0.2_linux-x64_bin.tar.gz
Once the download is complete, extract the downloaded file with the following command:
tar -xvf openjdk-16.0.2_linux-x64_bin.tar.gz
Next, move the extracted directory to the /opt with the following command:
mv jdk-16.0.2 /opt/
Next, you will need to add the Java path to the .bashrc file.
Edit the .bashrc file with the following command:
nano ~/.bashrc
Add the following lines at the end of the file:
export JAVA_HOME=/opt/jdk-16.0.2 export PATH=$PATH:$JAVA_HOME/bin
Save and close the file then activate the Java with the following command:
source ~/.bashrc
Next, verify the Java version with the following command:
java --version
You should get the following output:
java version "16.0.2" 2021-07-20 Java(TM) SE Runtime Environment (build 16.0.2+7-67) Java HotSpot(TM) 64-Bit Server VM (build 16.0.2+7-67, mixed mode, sharing)
Install Oracle JDK 16
First, visit the Oracle Java website obtain the download URL, and download it using the following command:
wget --no-check-certificate -c --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/16.0.2%2B7/d4a915d82b4c4fbb9bde534da945d746/jdk-16.0.2_linux-x64_bin.rpm
Once the download is complete, install the downloaded file using the following command:
rpm -ivh jdk-16.0.2_linux-x64_bin.rpm
You should see the following output:
warning: jdk-16.0.2_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:jdk-16.0.2-2000:16.0.2-ga ################################# [100%]
Next, you will need to set a Java path in the /etc/profile.d directory.
Create a new file named java.sh with the following command:
nano /etc/profile.d/java.sh
Add the Oracle Java installation location as shown below:
JAVA_HOME="/usr/java/jdk-16.0.2/bin/java"
Save and close the file then activate the Java environment variable with the following command:
source /etc/profile
Next, verify the Java version using the following command:
java -version
You should see the following output:
java version "16.0.2" 2021-07-20 Java(TM) SE Runtime Environment (build 16.0.2+7-67) Java HotSpot(TM) 64-Bit Server VM (build 16.0.2+7-67, mixed mode, sharing)
Conclusion
In the above guide, you learned how to install OpenJDK 16 and Oracle JDK 16 on Rocky Linux 8. You can now start developing your first Java application using Java.