How to install Java Development Kit (JDK) on CentOS, Red Hat and Fedora
Java Development Kit (JDK) is used for developing and compiling Java applications. Installing a JDK will also install the Java Runtime Environment (JRE), which is necessary for running Java applications.
Some of the JDKs you can choose to install are those provided by OpenJDK or Oracle. OpenJDK JDK is an open-source implementation of the Java SE platform, while Oracle JDK is the official version from Oracle. Both versions are similar, but Oracle JDK may have some additional features that are not available in OpenJDK JDK. This guide will show you how to install OpenJDK's JDK.
You can install OpenJDK's JDK on CentOS, Red Hat, and Fedora using yum or dnf at the terminal. There are also multiple versions of JDK available in the default yum repository that you can choose to install.
Steps to install Java Development Kit (JDK) on CentOS & Red Hat:
-
Launch terminal.
-
Search for available JDK versions for your distribution and version.
$ dnf search openjdk-devel Last metadata expiration check: 0:00:17 ago on Thu 26 Jan 2023 03:29:49 PM +08. ========================= Name Matched: openjdk-devel ========================== java-1.8.0-openjdk-devel.aarch64 : OpenJDK 8 Development Environment java-11-openjdk-devel.aarch64 : OpenJDK 11 Development Environment java-17-openjdk-devel.aarch64 : OpenJDK 17 Development Environment
-
Install preferred OpenJDK's JDK version for your system.
$ sudo dnf install --assumeyes java-1.8.0-openjdk-devel [sudo] password for user: CentOS Stream 9 - BaseOS 256 kB/s | 4.7 MB 00:18 CentOS Stream 9 - AppStream 689 kB/s | 13 MB 00:19 CentOS Stream 9 - Extras packages 644 B/s | 10 kB 00:15 Dependencies resolved. ================================================================================ Package Arch Version Repo Size ================================================================================ Installing: java-1.8.0-openjdk-devel aarch64 1:1.8.0.362.b01-0.3.ea.el9 appstream 9.3 M Installing dependencies: copy-jdk-configs noarch 4.0-3.el9 appstream 28 k ibus-gtk2 aarch64 1.5.25-2.el9 appstream 27 k java-1.8.0-openjdk aarch64 1:1.8.0.362.b01-0.3.ea.el9 appstream 456 k java-1.8.0-openjdk-headless aarch64 1:1.8.0.362.b01-0.3.ea.el9 appstream 33 M javapackages-filesystem noarch 6.0.0-3.el9 appstream 14 k lksctp-tools aarch64 1.0.19-2.el9 baseos 93 k lua aarch64 5.4.2-7.el9 appstream 185 k lua-posix aarch64 35.0-8.el9 appstream 149 k mkfontscale aarch64 1.2.1-3.el9 appstream 32 k ttmkfdir aarch64 3.0.9-65.el9 appstream 51 k tzdata-java noarch 2022g-2.el9 appstream 229 k xorg-x11-fonts-Type1 noarch 7.5-33.el9 appstream 505 k Installing weak dependencies: adwaita-gtk2-theme aarch64 3.28-14.el9 appstream 213 k gtk2 aarch64 2.24.33-8.el9 appstream 3.4 M libcanberra-gtk2 aarch64 0.30-26.el9 appstream 25 k Transaction Summary ================================================================================ Install 16 Packages Total download size: 48 M Installed size: 182 M ##### snipped
-
Check installed javac version.
$ javac -version javac 1.8.0_362-ea
-
Install other available JDK version if required.
$ sudo dnf install --assumeyes java-17-openjdk-devel [sudo] password for user: Last metadata expiration check: 0:04:43 ago on Thu 26 Jan 2023 03:32:34 PM +08. Dependencies resolved. ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: java-17-openjdk-devel aarch64 1:17.0.6.0.9-0.3.ea.el9 appstream 4.7 M Installing dependencies: java-17-openjdk aarch64 1:17.0.6.0.9-0.3.ea.el9 appstream 430 k java-17-openjdk-headless aarch64 1:17.0.6.0.9-0.3.ea.el9 appstream 41 M Transaction Summary ================================================================================ Install 3 Packages Total download size: 46 M Installed size: 206 M ##### snipped
-
Set default javac version if multiple versions are installed.
$ sudo alternatives --config javac There are 2 programs which provide 'javac'. Selection Command ----------------------------------------------- *+ 1 java-1.8.0-openjdk.aarch64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b01-0.3.ea.el9.aarch64/bin/javac) 2 java-17-openjdk.aarch64 (/usr/lib/jvm/java-17-openjdk-17.0.6.0.9-0.3.ea.el9.aarch64/bin/javac) Enter to keep the current selection[+], or type selection number: 2
-
Check current default JDK version to verify.
$ javac -version javac 17.0.6-ea