signed

QiShunwang

“诚信为本、客户至上”

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

2021/3/21 0:16:02   来源:

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

因为老项目所以装一个jdk1.7,CentOS

检查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
#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_HOME=${JAVA_PATH%/jre/bin*}  # shell语法参考{var%.*}
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/jre/lib/rt.jar
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME CLASSPATH PATH

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

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

切换JDK

当前JDK1.7

// 当前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.8,并重新加载环境变量

// 切换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 ~]#