How to install Jdk in centos
Categories:
How to Install JDK (Java Development Kit) on CentOS

A comprehensive guide to installing and configuring the Java Development Kit (JDK) on CentOS, covering both OpenJDK and Oracle JDK installations.
The Java Development Kit (JDK) is an essential component for developing and running Java applications. This article provides a step-by-step guide on how to install JDK on CentOS, covering both the open-source OpenJDK and the proprietary Oracle JDK. We'll also cover how to set up environment variables and manage multiple Java versions.
Understanding JDK Options: OpenJDK vs. Oracle JDK
Before proceeding with the installation, it's important to understand the differences between OpenJDK and Oracle JDK. OpenJDK is a free and open-source implementation of the Java Platform, Standard Edition (Java SE). Oracle JDK is the official, commercial build of Java SE from Oracle. While they are largely similar, Oracle JDK may include some proprietary features or optimizations. For most development and production environments, OpenJDK is a perfectly suitable and often preferred choice due to its open-source nature and licensing.
flowchart TD A[Start] --> B{Choose JDK Type?} B -->|OpenJDK| C[Install OpenJDK] B -->|Oracle JDK| D[Install Oracle JDK] C --> E[Set Default Java Version] D --> E E --> F[Verify Installation] F --> G[End]
JDK Installation Workflow on CentOS
Method 1: Installing OpenJDK on CentOS
OpenJDK is readily available in the CentOS repositories, making it the easiest and recommended way to install JDK. This method ensures that your Java installation is managed by the system's package manager, simplifying updates and maintenance.
1. Step 1: Update System Packages
Always start by updating your system's package list to ensure you get the latest available packages and dependencies.
2. Step 2: Search for Available OpenJDK Versions
Use yum
to search for available OpenJDK packages. This will show you the different versions you can install.
3. Step 3: Install OpenJDK
Choose the desired OpenJDK version (e.g., Java 11 or Java 8) and install it. For Java 11, use the following command:
4. Step 4: Verify Installation
After installation, verify that Java has been installed correctly and check its version.
sudo yum update -y
yum search openjdk
sudo yum install java-11-openjdk-devel -y
java -version
Commands to install and verify OpenJDK 11
java-11-openjdk-devel
with java-1.8.0-openjdk-devel
in the installation command.Method 2: Installing Oracle JDK on CentOS
Installing Oracle JDK requires downloading the binary from the Oracle website and manually configuring it. This method is suitable if you specifically need Oracle's proprietary features or have licensing requirements. Remember that Oracle JDK's licensing has changed, and commercial use may require a subscription.
1. Step 1: Download Oracle JDK
Visit the official Oracle Java download page (oracle.com/java/technologies/downloads) and download the appropriate .rpm
package for CentOS. You will likely need an Oracle account to download.
2. Step 2: Install the RPM Package
Navigate to the directory where you downloaded the .rpm
file and install it using yum
.
3. Step 3: Verify Installation
Check the installed Java version to confirm a successful installation.
cd ~/Downloads
sudo yum localinstall jdk-*-linux-x64.rpm
java -version
Commands to install Oracle JDK from an RPM package
Managing Multiple Java Versions with alternatives
CentOS uses the alternatives
system to manage different versions of the same command. This is particularly useful if you have multiple JDKs installed and need to switch between them.
1. Step 1: List Available Java Alternatives
See all installed Java versions that the alternatives
system is aware of.
2. Step 2: Set Default Java Version
Use the alternatives --config java
command to interactively select your preferred default Java version. You will be prompted to enter a selection number.
3. Step 3: Verify the Change
Confirm that the default Java version has been updated.
sudo alternatives --config java
java -version
Commands to manage Java versions using alternatives
alternatives
system also manages javac
and jar
commands. When you set the default java
version, it typically updates these as well.Setting JAVA_HOME Environment Variable
Many Java applications and build tools (like Maven or Gradle) rely on the JAVA_HOME
environment variable to locate the JDK installation. It's good practice to set this variable.
1. Step 1: Find Your JDK Installation Path
The path depends on whether you installed OpenJDK or Oracle JDK. For OpenJDK, it's usually under /usr/lib/jvm/
.
2. Step 2: Edit the Environment Profile
Open the /etc/profile
file (for system-wide setting) or ~/.bashrc
(for user-specific setting) with a text editor.
3. Step 3: Add JAVA_HOME and PATH
Add the following lines to the end of the file, replacing /path/to/your/jdk
with your actual JDK installation path.
4. Step 4: Apply Changes
Source the profile file to apply the changes immediately without rebooting.
5. Step 5: Verify JAVA_HOME
Check if the JAVA_HOME
variable is correctly set.
readlink -f /usr/bin/java
sudo vi /etc/profile
# Add these lines:
export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-11.0.12.0.7-0.el8_4.x86_64"
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile
echo $JAVA_HOME
Setting the JAVA_HOME environment variable for OpenJDK 11