OpenJDK Installation on CentOS Stream 9 : Beginner’s Guide

How to install OpenJDK on Linux 1

On this short article we will show you how to install  OpenJDK  8, OpenJDK 11, OpenJDK 17 and OpenJDK 21 on CentOS Stream 9.  OpenJDK (Open Java Development Kit) is an essential component of the modern software ecosystem. It serves as the open-source implementation of the Java Platform, Standard Edition (Java SE), providing developers with a free and robust environment to build, test, and deploy Java-based applications. OpenJDK (Open Java Development Kit) is an open-source implementation of the Java Platform. It is essential for running Java-based applications. Below are the steps to install OpenJDK on CentOS Stream 9 :

1. Update Your System
2. Check Available OpenJDK Versions
3. Install Your Preferred OpenJDK Version
4. Verify the Installation
5. Configure Default Java Version (Optional)
6. Set JAVA_HOME Environment Variable

Step 1: Update Your System

OpenJDK (Open Java Development Kit) is an open-source implementation of the Java Platform. It is essential for running Java-based applications. Below are the steps to install OpenJDK on CentOS Stream.

$ sudo dnf update -y

Step 2: Check Available OpenJDK Versions

Use the following command to list the available OpenJDK versions:

$ sudo dnf search openjdk

You’ll see a list of available versions, such as:

java-1.8.0-openjdk
java-11-openjdk
java-17-openjdk
java-21-openjdk

OpenJDK version list

Step 3: Install Your Preferred OpenJDK Version

1. Install OpenJDK 11 (default for many systems)

We will use OpenJDK 11 for majority applications which is live on our system, to install OpenJDK 11 version we just hit the command line :

$ sudo dnf install java-11-openjdk java-11-openjdk-devel -y

2. Install OpenJDK 8 (for legacy applications):

$ sudo dnf install java-1.8.0-openjdk java-1.8.0-openjdk-devel -y

3. Install OpenJDK 17 and OpenJDK 21 (latest LTS version):

$ sudo dnf install java-17-openjdk java-17-openjdk-devel -y
$ sudo dnf install java-21-openjdk java-21-openjdk-devel -y

Step 4: Verify the Installation

After we have already installed all OpenJDK version, the we will check the installed Java version by submitting command line :

$ java -version

The sample output will be as shown :

[ramansah@nodeman ~]$ java -version
openjdk version "11.0.20.1" 2023-08-24 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.20.1.1-2) (build 11.0.20.1+1-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.20.1.1-2) (build 11.0.20.1+1-LTS, mixed mode, sharing)

and we will ensure the javac compiler is also already installed, by submitting command line :

 $ javac -version

The sampel output will be shown :

[ramansah@nodeman ~]$ javac -version
javac 11.0.20.1

Step 5: Configure Default Java Version (Optional)

If multiple Java versions are installed, set the default version using alternatives command line.

1. List installed Java versions

The sample is as shown below :

$ sudo alternatives --config java

The sample output will be as shown below :

[ramansah@nodeman ~]$ sudo alternatives --config java

There are 4 programs which provide 'java'.

Selection Command
-----------------------------------------------
+ 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.20.1.1-2.el9.x86_64/bin/java)
* 2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.13.0.11-4.el9.x86_64/bin/java)
3 java-21-openjdk.x86_64 (/usr/lib/jvm/java-21-openjdk-21.0.5.0.11-2.el9.x86_64/bin/java)
4 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b09-4.el9.x86_64/jre/bin/java)

Select OpenJDK version

2 Select the version you want as the default by entering its number.

By inputing the selection number, we could chose the number related to the OpenJDK version. On the sample above, we have chosen the number ‘3’, OpenJDK version 21, while the current OpenJDK version was OpenJDK 17.

Step 6: Set JAVA_HOME Environment Variable

To set up the JAVA_HOME environment variable, we will use the following steps :

1. Find the Java installation directory:

$ sudo update-alternatives --display java

Example path: /usr/lib/jvm/java-11-openjdk-11.0.20.0.8-3.el9.x86_64.

2. Add the environment variable to the profile:

$ sudo vi /etc/profile.d/java.sh

3. Add the following lines:

export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.20.0.8-3.el9.x86_64
export PATH=$JAVA_HOME/bin:$PATH

4. Save and close the file (Ctrl + O, Enter, Ctrl + X).

5. Apply the changes:

$ source /etc/profile.d/java.sh

6. Verify the JAVA_HOME variable:

$ echo $JAVA_HOME

Conclusion

You’ve successfully installed and configured OpenJDK on CentOS Stream. Your system is now ready for Java development and running Java applications.

 

(Visited 62 times, 1 visits today)

You may also like