Please register your RHEL version with RedHat first
First open Terminal and change permission to root then enter you password.
su -
You can skip this step if there is no installed JDK on you computer.
Run these 2 commands to check if there is any installed JDK.
rpm -qa | grep java
rpm -qa | grep jdk
Note: The correct installed JDKs should shows on both commands result.
Uninstall installed JDKs (if exist). For example, I have 2 installed JDKs need to remove that have named as
- java-1.8.0-openjdk-headless-1.8.0.181-3.b13.el7_5.x86_64
- java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64
Then I can remove them by command yum remove [version_name]
, like this
yum remove java-1.8.0-openjdk-headless-1.8.0.181-3.b13.el7_5.x86_64
yum remove java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64
First download JDK version that you want to install, for example jdk1.7.0_80
Create a JDK folder in /opt
and extract downloaded package to it.
mkdir /opt/jdk
tar -xvzf /media/psf/Home/Downloads/jdk-7u80-linux-x64.tar.gz -C /opt/jdk
Setting JDK as the default JVM, you need to do it for both java and javac
update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.7.0_80/bin/java 100
update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk1.7.0_80/bin/javac 100
Verify your installation
update-alternatives --display java
update-alternatives --display javac
java -version
You should see the following result
java - status is auto.
link currently points to /opt/jdk/jdk1.7.0_80/bin/java
/opt/jdk/jdk1.7.0_80/bin/java - priority 100
Current `best` version is /opt/jdk/jdk1.7.0_80/bin/java.
javac - status is auto.
link currently points to /opt/jdk/jdk1.7.0_80/bin/javac
/opt/jdk/jdk1.7.0_80/bin/javac - priority 100
Current `best` version is /opt/jdk/jdk1.7.0_80/bin/javac.
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
For example, you want to update new jdk1.8.0_181 version, then run install command with higher priority number
update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.8.0_181/bin/java 110
update-alternatives --install /usr/bin/javac javac /opt/jdk/jdk1.8.0_181/bin/javac 110
Remove the old version by these commands, for example jdk1.7.0_80
update-alternatives --remove java /opt/jdk/jdk1.7.0_80/bin/java
update-alternatives --remove javac /opt/jdk/jdk1.7.0_80/bin/javac
#Remove the old version directory
rm -rf /opt/jdk/jdk1.7.0_80