Follow through this tutorial to quickly learn how to install Java 11|Java 17|Java 18 on Rocky Linux.
Install Java 11|17|18 on Rocky Linux
There are two versions of Java; OpenJDK and Oracle JDK;
While both are products of Oracle, OpenJDK is an opensource version while Oracle JDK is a enterprise product that requires a commercial license to use.
- Install OpenJDK 11|OpenJDK 17|OpenJDK 18 on Rocky Linux
- How to Set Default Java Version on Rocky Linux
Install OpenJDK 11|OpenJDK 17|OpenJDK 18 on Rocky Linux
OpenJDK 11|OpenJDK 17|OpenJDK are available on the default Rocky Linux Universe repositories.
dnf search jdk | egrep -- '-11|-17|-18'
Sample output;
java-11-openjdk.x86_64 : OpenJDK 11 Runtime Environment
java-11-openjdk-demo.x86_64 : OpenJDK 11 Demos
java-11-openjdk-devel.x86_64 : OpenJDK 11 Development Environment
java-11-openjdk-headless.x86_64 : OpenJDK 11 Headless Runtime Environment
java-11-openjdk-javadoc.x86_64 : OpenJDK 11 API documentation
java-11-openjdk-javadoc-zip.x86_64 : OpenJDK 11 API documentation compressed in a single archive
java-11-openjdk-jmods.x86_64 : JMods for OpenJDK 11
java-11-openjdk-src.x86_64 : OpenJDK 11 Source Bundle
java-11-openjdk-static-libs.x86_64 : OpenJDK 11 libraries for static linking
java-17-openjdk.x86_64 : OpenJDK 17 Runtime Environment
java-17-openjdk-demo.x86_64 : OpenJDK 17 Demos
java-17-openjdk-devel.x86_64 : OpenJDK 17 Development Environment
java-17-openjdk-headless.x86_64 : OpenJDK 17 Headless Runtime Environment
java-17-openjdk-javadoc.x86_64 : OpenJDK 17 API documentation
java-17-openjdk-javadoc-zip.x86_64 : OpenJDK 17 API documentation compressed in a single archive
java-17-openjdk-jmods.x86_64 : JMods for OpenJDK 17
java-17-openjdk-src.x86_64 : OpenJDK 17 Source Bundle
java-17-openjdk-static-libs.x86_64 : OpenJDK 17 libraries for static linking
To install OpenJDK 11 on Rocky Linux, execute the commands below;
dnf install java-11-openjdk java-11-openjdk-devel
Confirm the version;
java --version
openjdk 11.0.15 2022-04-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.15+9-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.15+9-LTS, mixed mode, sharing)
To install OpenJDK 17 on Rocky Linux, execute the commands below;
dnf install java-17-openjdk java-17-openjdk-devel
Confirm the version;
java --version
openjdk 17.0.3 2022-04-19 LTS
OpenJDK Runtime Environment 21.9 (build 17.0.3+6-LTS)
OpenJDK 64-Bit Server VM 21.9 (build 17.0.3+6-LTS, mixed mode, sharing)
As of this writing, Java 18 is not available on the default Rocky Linux repositories. Thus, to install OpenJDK 18 on Rocky Linux;
Download Java 18 RPM from the downloads’ page and install it using the command below
dnf install https://download.oracle.com/java/18/latest/jdk-18_linux-x64_bin.rpm
Confirm the version;
java --version
java 18.0.1.1 2022-04-22
Java(TM) SE Runtime Environment (build 18.0.1.1+2-6)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)
How to Set Default Java Version on Rocky Linux
If you have multiple versions of Java installed and want to set either Java 11|Java 17|Java 18 as the default, run the command below;
sudo update-alternatives --config java
Sample output;
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.9-2.el8_5.x86_64/bin/java)
2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.3.0.6-2.el8_5.x86_64/bin/java)
*+ 3 /usr/java/jdk-18.0.1.1/bin/java
Enter to keep the current selection[+], or type selection number:
From the output above, you can see that Java 18 is the current default Java version set.
For example, I would enter number 2 if i want to set Java 17 as the default version.
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.9-2.el8_5.x86_64/bin/java)
2 java-17-openjdk.x86_64 (/usr/lib/jvm/java-17-openjdk-17.0.3.0.6-2.el8_5.x86_64/bin/java)
*+ 3 /usr/java/jdk-18.0.1.1/bin/java
Enter to keep the current selection[+], or type selection number: 2
To set another default version, rerun the sudo update-alternatives --config java
command choose the enter the number of your selection and press ENTER from the above command output.
Set JAVA HOME Environment Variable
Next you can set Java home environment variable of your Java selection by updating the path to the Java version.
For example, current version of Java selected;
java --version
openjdk 17.0.3 2022-04-19 LTS
OpenJDK Runtime Environment 21.9 (build 17.0.3+6-LTS)
OpenJDK 64-Bit Server VM 21.9 (build 17.0.3+6-LTS, mixed mode, sharing)
The path can be obtained by rerunning the command above or by just running the command below;
readlink -f /usr/bin/java | sed "s:jre/bin/java::"
Sample output;
/usr/lib/jvm/java-17-openjdk-17.0.3.0.6-2.el8_5.x86_64/bin/java
Next, update the /etc/environment file to load the environment variables.
echo "JAVA_HOME=/usr/lib/jvm/java-17-openjdk-17.0.3.0.6-2.el8_5.x86_64/bin/java" >> /etc/environment
source /etc/environment
To confirm the Java home environment variable setting, run the command below;
echo $JAVA_HOME
/usr/lib/jvm/java-17-openjdk-17.0.3.0.6-2.el8_5.x86_64/bin/java
Well, you are good to go.
That is all on how to install Java 11|Java 17|Java 18 Rocky Linux.
See other related guides by following the links below;
Install Java 11|Java 17|Java 18 on Ubuntu 22.04