On CentOs we need to install JDK for some programs like Ant, Maven and etc. If you are already installed Java JDK and you want to update it, you can start from Section 0.
Section 0 — Check and Remove old versions
Firstly update your server
yum update
Then, check if any older JDK versions are installed in your system.
rpm -qa | grep -E '^open[jre|jdk]|j[re|dk]'
Check Java version
java -version
If there are other versions you can remove them via this command
yum remove java-1.8.0-openjdkor yum remove jdk-1.8.0
Note: Instead of 1.8.0 you can write your version
Section 1 — Install Oracle Java JDK (version 1)
cd ~/Downloadswget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn-pub/java/jdk/16.0.2%2B7/d4a915d82b4c4fbb9bde534da945d746/jdk-16.0.2_linux-x64_bin.rpm"
Note: You can change this download link and install another version. For another version go there
For example: You can download JDK 11 like this
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn/java/jdk/11.0.12%2B8/f411702ca7704a54a79ead0c2e0942a3/jdk-11.0.12_linux-x64_bin.rpm"
Extract Jdk 16
rpm -ivh jdk-16.0.2_linux-x64_bin.rpm
Check Java version
java -version
Output:
java version “16.0.2” 2021–07–20
Java(TM) SE Runtime Environment (build 16.0.2+7–67)
Java HotSpot(TM) 64-Bit Server VM (build 16.0.2+7–67, mixed mode, sharing)
Setup Global Environment Variables
We can easily set the environment variables using the export command as shown below.
export JAVA_HOME=/usr/java/jdk-16.0.2/
export PATH=$PATH:$JAVA_HOME
Now, let us check for the environment variables using commands:
echo $JAVA_HOME
Output:
/usr/java/jdk-16.0.2/
The path will be disappear when the system reboots so to make it permanent, you have to add the paths in the system profile.
Create a file called java.sh under /etc/profile.d/ directory.
nano /etc/profile.d/java.sh#!/bin/bash
JAVA_HOME=/usr/java/jdk-16.0.2/
PATH=$JAVA_HOME/bin:$PATH
export PATH JAVA_HOME
export CLASSPATH=.
Save and close the file.
Make it executable :
chmod +x /etc/profile.d/java.sh
Then, set the environment variables permanent
source /etc/profile.d/java.sh
That’s it.
Section 2— Install Oracle Java JDK (version 2)
cd ~/Downloadswget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "https://download.oracle.com/otn-pub/java/jdk/16.0.2%2B7/d4a915d82b4c4fbb9bde534da945d746/jdk-16.0.2_linux-x64_bin.rpm"
Note: You can change this download link and install another version. For another version go there
sudo yum localinstall jdk-16.0.2_linux-x64_bin.rpm
Now Java should be installed at /usr/java/jdk-16.0.2, and linked from /usr/bin/java.
You may delete the archive file that you downloaded earlier:
rm ~/Downloads/jdk-16.0.2_linux-x64_bin.rpm
Congratulations! You have installed Oracle Java 16 JDK.

所有评论(0)