服务器centos7
1.查看java版本
[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)
可以看到系统自带的是openjdk现在需要卸载openjdk,然后安装jdk1.8
2.找到openjdk并卸载
删除时要注意顺序,第二个是第一个的依赖,所以直接先卸载第一个(java-1.8.0-openjdk-headless-1.8.0.222.b10-1.el7_7.x86_64)会报错,会告诉你它有依赖在用删不掉,所以我们需要先删除java-1.8.0-openjdk-1.8.0.222.b10-1.el7_7.x86_64,然后在删除java-1.8.0-openjdk-headless-1.8.0.222.b10-1.el7_7.x86_64。如下所示:
[root@localhost ~]# rpm -qa |grep openjdk
java-1.8.0-openjdk-headless-1.8.0.222.b10-1.el7_7.x86_64
java-1.8.0-openjdk-1.8.0.222.b10-1.el7_7.x86_64
[root@localhost ~]# rpm -e java-1.8.0-openjdk-headless-1.8.0.222.b10-1.el7_7.x86_64
error: Failed dependencies:
java-1.8.0-openjdk-headless(x86-64) = 1:1.8.0.222.b10-1.el7_7 is needed by (installed) java-1.8.0-openjdk-1:1.8.0.222.b10-1.el7_7.x86_64
libjava.so()(64bit) is needed by (installed) java-1.8.0-openjdk-1:1.8.0.222.b10-1.el7_7.x86_64
libjava.so(SUNWprivate_1.1)(64bit) is needed by (installed) java-1.8.0-openjdk-1:1.8.0.222.b10-1.el7_7.x86_64
libjvm.so()(64bit) is needed by (installed) java-1.8.0-openjdk-1:1.8.0.222.b10-1.el7_7.x86_64
libjvm.so(SUNWprivate_1.1)(64bit) is needed by (installed) java-1.8.0-openjdk-1:1.8.0.222.b10-1.el7_7.x86_64
[root@iZ2zealy7077klzk8hl66rZ ~]# rpm -e java-1.8.0-openjdk-1.8.0.222.b10-1.el7_7.x86_64
[root@iZ2zealy7077klzk8hl66rZ ~]# rpm -e java-1.8.0-openjdk-headless-1.8.0.222.b10-1.el7_7.x86_64
验证是否删除成功
[root@iZ2zealy7077klzk8hl66rZ ~]# java -version
-bash: /usr/bin/java: No such file or directory
3.安装jdk1.8
将准备好的安装包放入服务器中
[root@localhost ~]# tar xf jdk-8u161-linux-x64.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv jdk1.8.0_161/ jdk1.8
修改环境变量
[root@localhost local]# vim /etc/profile
在末尾加
export JAVA_HOME=/usr/local/jdk1.8
export PATH=$PATH:$JAVA_HOME/bin
[root@localhost local]# source /etc/profile
检查是否成功
[root@localhost local]# java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)