1.卸载旧版本
# 查看系统是否自带 jdk
rpm -qa |grep java
rpm -qa |grep jdk
rpm -qa |grep gcj
# 如果有输出信息,批量卸载系统自带
rpm -qa | grep java | xargs rpm -e --nodeps
# 如果使用 yum 安装的 jdk,请使用下面命令卸载
yum -y remove java-1.8.0-openjdk-headless.x86_64
2.安装JDK
# 检查 yum 中有没有 java1.8 包
$ yum list java-1.8*
java-1.8.0-openjdk.x86_64 1:1.8.0.272.b10-3.el8_3 AppStream
java-1.8.0-openjdk-accessibility.x86_64 1:1.8.0.272.b10-3.el8_3 AppStream
java-1.8.0-openjdk-demo.x86_64 1:1.8.0.272.b10-3.el8_3 AppStream
java-1.8.0-openjdk-devel.x86_64 1:1.8.0.272.b10-3.el8_3 AppStream
java-1.8.0-openjdk-headless.x86_64 1:1.8.0.272.b10-3.el8_3 AppStream
java-1.8.0-openjdk-javadoc.noarch 1:1.8.0.272.b10-3.el8_3 AppStream
java-1.8.0-openjdk-javadoc-zip.noarch 1:1.8.0.272.b10-3.el8_3 AppStream
java-1.8.0-openjdk-src.x86_64 1:1.8.0.272.b10-3.el8_3 AppStream
# 开始安装
yum install java-1.8.0-openjdk* -y
3.验证
# jdk的默认安装位置为/usr/lib/jvm
[root@localhost ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
4.配置环境变量
# 查看安装的jdk路径
cd /usr/lib/jvm/
# 配置
vi /etc/profile
# java
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.el7_9.x86_64
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
# 保存并退出
:wq
# 配置文件生效
source /etc/profile