CentOS安装多个JDK版本、自动切换环境变量

老项目使用JDK1.7,因此系统中安装多个JDK版本,为了便于切换,修改了/etc/profile文件,每次加载文件自动获取对应JDK版本的环境变量。

检查Java 版本

// 检查系统java 版本
[root@centos7-18 data]# java -version
openjdk version "1.8.0_272"
OpenJDK Runtime Environment (build 1.8.0_272-b10)
OpenJDK 64-Bit Server VM (build 25.272-b10, mixed mode)
[root@centos7-18 data]# 

检查yum java 安装版本

[root@centos7-18 data]# yum list | grep java-1.7
java-1.7.0-openjdk.x86_64                1:1.7.0.261-2.6.22.2.el7_8     @base   
java-1.7.0-openjdk-devel.x86_64          1:1.7.0.261-2.6.22.2.el7_8     @base   
java-1.7.0-openjdk-headless.x86_64       1:1.7.0.261-2.6.22.2.el7_8     @base   
java-1.7.0-openjdk-accessibility.x86_64  1:1.7.0.261-2.6.22.2.el7_8     base    
java-1.7.0-openjdk-demo.x86_64           1:1.7.0.261-2.6.22.2.el7_8     base    
java-1.7.0-openjdk-javadoc.noarch        1:1.7.0.261-2.6.22.2.el7_8     base    
java-1.7.0-openjdk-src.x86_64            1:1.7.0.261-2.6.22.2.el7_8     base    
[root@centos7-18 data]# 

安装JDK 1.7

yum -y install java-1.7.0-openjdk-devel.x86_64

设置JDK环境变量

vim /etc/profile

根据当前版本java命令路径,获取对应的JAVA_HOME路径

#export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.252.b09-2.el7_8.x86_64
#JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk  # 注意路径和文件名
// 在/etc/profile定义,每次根据当前java 切换不同的Java环境变量
JAVA_PATH=$(readlink -f /usr/bin/java)  # 获取当前java命令路径
JAVA_HOME=${JAVA_PATH%/jre/bin*}  # 截取JAVA_HOME路径,shell语法参考{var%.*}
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH

shell语法链接: shell之文件路径截取.

// 环境变量生效
source /etc/profile
// 验证环境变量
echo $JAVA_HOME && echo $CLASSPATH && echo $PATH

切换JDK

当前JDK1.8

// 当前JDK版本及环境变量
[root@centos7-18 ~]# java -version
openjdk version "1.8.0_272"
OpenJDK Runtime Environment (build 1.8.0_272-b10)
OpenJDK 64-Bit Server VM (build 25.272-b10, mixed mode)
[root@centos7-18 ~]# 
[root@centos7-18 ~]# echo $JAVA_HOME && echo $CLASSPATH && echo $PATH
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64
.:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64/lib/dt.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64/lib/tools.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64/jre/lib/rt.jar
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/lib/jvm/java-1.8.0-openjdk/bin:/root/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64/bin:/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64/bin
[root@centos7-18 ~]# 

切换JDK1.7,并重新加载环境变量

// 切换JDK1.8
[root@centos7-18 ~]# alternatives --config java

共有 2 个提供“java”的程序。

  选项    命令
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64/jre/bin/java)
   2           java-1.7.0-openjdk.x86_64 (/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64/jre/bin/java)

按 Enter 保留当前选项[+],或者键入选项编号:2
[root@centos7-18 ~]# 

// 重新加载环境变量
[root@centos7-18 ~]# source /etc/profile
[root@centos7-18 ~]# 

检查切换结果

// 检查JDK 版本
[root@centos7-18 ~]# java -version
java version "1.7.0_261"
OpenJDK Runtime Environment (rhel-2.6.22.2.el7_8-x86_64 u261-b02)
OpenJDK 64-Bit Server VM (build 24.261-b02, mixed mode)
[root@centos7-18 ~]# 
// 检查环境变量
[root@centos7-18 ~]# echo $JAVA_HOME && echo $CLASSPATH && echo $PATH
/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64
.:/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64/lib/dt.jar:/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64/lib/tools.jar:/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64/jre/lib/rt.jar
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/lib/jvm/java-1.8.0-openjdk/bin:/root/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64/bin:/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.el7_9.x86_64/bin:/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.261-2.6.22.2.el7_8.x86_64/bin
[root@centos7-18 ~]# 

你可能感兴趣的:(运维,JAVA,jdk,java,linux)