1.卸载之前的jdk
卸载方式:(之前下载的jdk1.8.0-161在/etc/profile中已有修改环境变量的命令,卸载时无需修改环境变量)
(1)找出之前下载的jdk版本
[root@one01 ~]# rpm -qa | grep -i java
javapackages-tools-3.4.1-11.el7.noarch
java-1.8.0-openjdk-headless-1.8.0.161-2.b14.el7.x86_64
python-javapackages-3.4.1-11.el7.noarch
tzdata-java-2018c-1.el7.noarch
(2) 查看之前下载的版本号(目前用的jdk1.8.0_161版本)
[root@one01 ~]# java -version
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)
(3) 卸载之前的版本(版本号为上面所查询到的已有的)
[root@one01 ~]# yum -y remove java-1.8.0-openjdk-headless-1.8.0.161-2.b14.el7.x86_64
[root@one01 ~]# java -version //卸载完后查看,没有了
-bash: /usr/bin/java: 没有那个文件或目录
2.安装jdk22版本以及更高版本
(1)去官网(Java Downloads | Oracle 中国)上查看自己需要的jdk安装文件并下载,此处我选用的是https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.tar.gz
[root@one01 ~]# ls
anaconda-ks.cfg jdk-22_linux-x64_bin.tar.gz
(2)解压
[root@one01 ~]# tar -zxvf jdk-22_linux-x64_bin.tar.gz
[root@one01 ~]# ls
anaconda-ks.cfg jdk-22.0.1 jdk-22_linux-x64_bin.tar.gz
(3)移动jdk文件并查询其路径
[root@one01 ~]# mv jdk-22.0.1/ /usr/local/
[root@one01 ~]# cd /usr/local/
[root@one01 local]# ls
apache-tomcat-10.1.25 bin games jdk-22.0.1 lib64 sbin src
apache-tomcat-9.0.91 etc include lib libexec share
[root@one01 local]# cd /usr/local/jdk-22.0.1/
[root@one01 jdk-22.0.1]# cd bin/
[root@one01 bin]# pwd
/usr/local/jdk-22.0.1/bin
(4)修改环境变量
[root@one01 bin]# vim /etc/profile
export PATH=/usr/local/jdk-22.0.1/bin:$PATH //末尾添加暴露路径
(5)刷新并查看
[root@one01 bin]# source /etc/profile
[root@one01 bin]# java -version
java version "22.0.1" 2024-04-16
Java(TM) SE Runtime Environment (build 22.0.1+8-16)
Java HotSpot(TM) 64-Bit Server VM (build 22.0.1+8-16, mixed mode, sharing)
3.在profile文件中配置jdk环境
(1)查看JDK的bin目录所在位置
[root@shixun ~]# cd /usr/local/jdk-22.0.1/bin/
[root@shixun bin]# pwd
/usr/local/jdk-22.0.1/bin
[root@shixun bin]#
(2)将JAVA的可执行文件添加到$PATH环境变量中
[root@shixun jdk-22.0.1]# vim /etc/profile
……
……
export PATH=/usr/local/jdk-22.0.1/bin:$PATH
[root@shixun jdk-22.0.1]# source /etc/profile
(3)配置JAVA_HOME=/usr/local/jdk-22.0.1/
[root@shixun jdk-22.0.1]# vim /etc/profile
……
……
export JAVA_HOME=/usr/local/jdk-22.0.1/
[root@shixun jdk-22.0.1]# source /etc/profile //让其生效
[root@shixun jdk-22.0.1]# $JAVA_HOME
-bash: /usr/local/jdk-22.0.1/: 是一个目录
[root@shixun jdk-22.0.1]#
(4) 查看是否成功
[root@shixun jdk-22.0.1]# java -version
java version "22.0.1" 2024-04-16
Java(TM) SE Runtime Environment (build 22.0.1+8-16)
Java HotSpot(TM) 64-Bit Server VM (build 22.0.1+8-16, mixed mode, sharing)
[root@shixun jdk-22.0.1]# javac -version
javac 22.0.1
[root@shixun jdk-22.0.1]#