How To Install OpenJDK 17 on Centos 7

This tutorial intends to teach you to Install OpenJDK 17 (Java 17) on Centos 7.

OpenJDK is an open-source implementation of the Java Standard Edition (Java SE) and Java Development Kit (JDK). The OpenJDK source code is the basis for these commercial Java products. OpenJDK is available for free with a GNU General Public license. 

Steps To Install OpenJDK 17 (Java 17) on Centos 7

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Centos 7.

Install Java (JDK) 17 on Centos 7

First, you need to update your local package index with the command below:

sudo yum update -y

Then install the required packages by using the following command:

sudo yum install curl wget -y

Download OpenJDK 17

At this point, you need to visit the JDK Downloads page to download the latest archive of Java 17 by using the wget command:

sudo wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz

Then, extract your downloaded file with the command below:

sudo tar xvf openjdk-17.0.2_linux-x64_bin.tar.gz

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

sudo mv jdk-17.0.2 /opt/

Configure Java Environment Path

At this point, you need to configure the Java home path by using the following command:

sudo tee /etc/profile.d/jdk17.sh <<EOF
export JAVA_HOME=/opt/jdk-17.0.2
export PATH=\$PATH:\$JAVA_HOME/bin
EOF

Source your profile file with the following command:

source /etc/profile.d/jdk17.sh

Verify your Java Home path:

echo $JAVA_HOME
Output
/opt/jdk-17.0.2

Also, you can verify your Java installation by checking its version:

java -version
Output
openjdk version "17.0.2" 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

Install Java SE Development Kit 17 on Centos 7

If you choose to go with Java SE Development Kit 17, download the RPM package for CentOS / RHEL / Fedora system using the command below:

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

Then, install the RPM package using the yum or rpm command:

sudo rpm -Uvh jdk-17_linux-x64_bin.rpm
Output
warning: jdk-17_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk-17-2000:17.0.6-9             ################################# [100%]

Confirm the Java installation on Centos 7:

java -version
Output
openjdk version "17.0.2" 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

Configure the Java environment with the command below:

cat <<EOF | sudo tee /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/java/default
export PATH=\$PATH:\$JAVA_HOME/bin
EOF

To use Java Home, source the file by using the following command:

source /etc/profile.d/jdk.sh

Create a sample project with Java 17

At this point, we will show you to create a sample project to see that your Java 17 is working correctly on Centos 7.

Create and open the hello world file with your favorite text editor, here we use the vi editor:

vi HelloWorld.java

Add the following content to the file:

public class HelloWorld {

    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }

}

When you are done, save and close the file.

Then, compile and run your Java code:

java HelloWorld.java
Output
Hello, World

That’s it, you are done.

Conclusion

At this point, you have learned to Install OpenJDK 17 (Java 17) on Centos 7.

Hope you enjoy it. You may be like these articles:

Install and Configure Samba Share on Centos 7

How To Set up Apache Kafka on Centos 7

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!