很多时候我们需要不同的jdk环境,那就会有一个问题。如何动态切换jdk版本呢?
方法1 alternatives命令nodejs有nvm,但是jdk没有这样的工具。 但是没想到linux系统本身就带了一个类似版本切换的工具。
使用步骤解压jdk的安装包到同一个目录下,如
[root@6791a42bf857 data]# ll total 16 drwxr-xr-x 8 root root 4096 Jan 25 11:26 jdk-11.0.11 drwxr-xr-x 8 root root 4096 Jan 25 11:27 jdk-17.0.2 drwxr-xr-x 8 10 143 4096 Jul 22 2017 jdk1.8.0_144 [root@6791a42bf857 data]# [root@6791a42bf857 data]# alternatives --help alternatives version 1.3.61 - Copyright (C) 2001 Red Hat, Inc. This may be freely redistributed under the terms of the GNU Public License. usage: alternatives --installalternatives --install安装版本[--initscript ] [--slave ]* alternatives --remove # 移除 alternatives --auto # 设置为自动 alternatives --config # 交互配置某个版本 alternatives --display # 显示 alternatives --set # 非交互配置某个版本 alternatives --list # 列出所有的配置 common options: --verbose --test --help --usage --version --altdir --admindir [root@6791a42bf857 data]#
[root@6791a42bf857 data]# alternatives --install /usr/bin/java java /data/jdk-11.0.11/bin/java 2 [root@6791a42bf857 data]# [root@6791a42bf857 data]# alternatives --config java There is 1 program that provides 'java'. Selection Command ----------------------------------------------- *+ 1 /data/jdk1.8.0_144/bin/java [root@6791a42bf857 data]# alternatives --install /usr/bin/java java /data/jdk-11.0.11/bin/java 2 [root@6791a42bf857 data]# [root@6791a42bf857 data]# [root@6791a42bf857 data]# alternatives --install /usr/bin/java java /data/jdk-17.0.2/bin/java 3 [root@6791a42bf857 data]# [root@6791a42bf857 data]# [root@6791a42bf857 data]# [root@6791a42bf857 data]# alternatives --config java There are 3 programs which provide 'java'. Selection Command ----------------------------------------------- + 1 /data/jdk1.8.0_144/bin/java 2 /data/jdk-11.0.11/bin/java * 3 /data/jdk-17.0.2/bin/javaalternatives --config配置版本
[root@6791a42bf857 data]# alternatives --config java There are 3 programs which provide 'java'. Selection Command ----------------------------------------------- + 1 /data/jdk1.8.0_144/bin/java 2 /data/jdk-11.0.11/bin/java * 3 /data/jdk-17.0.2/bin/java Enter to keep the current selection[+], or type selection number: 3 [root@6791a42bf857 data]# alternatives --config java There are 3 programs which provide 'java'. Selection Command ----------------------------------------------- 1 /data/jdk1.8.0_144/bin/java 2 /data/jdk-11.0.11/bin/java *+ 3 /data/jdk-17.0.2/bin/java Enter to keep the current selection[+], or type selection number: [root@6791a42bf857 data]# java -version Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 openjdk version "17.0.2" 2022-01-18 OpenJDK Runtime Environment (build 17.0.2+8-86) OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)alternatives --display显示版本
[root@6791a42bf857 data]# alternatives --display java java - status is auto. link currently points to /data/jdk-17.0.2/bin/java /data/jdk1.8.0_144/bin/java - priority 1 /data/jdk-11.0.11/bin/java - priority 2 /data/jdk-17.0.2/bin/java - priority 3 Current `best' version is /data/jdk-17.0.2/bin/java.alternatives --set 设置当前版本
[root@6791a42bf857 data]# alternatives --set java /data/jdk-11.0.11/bin/java [root@6791a42bf857 data]# alternatives --display java java - status is manual. link currently points to /data/jdk-11.0.11/bin/java /data/jdk1.8.0_144/bin/java - priority 1 /data/jdk-11.0.11/bin/java - priority 2 /data/jdk-17.0.2/bin/java - priority 3 Current `best' version is /data/jdk-17.0.2/bin/java. [root@6791a42bf857 data]# [root@6791a42bf857 data]# echo 3 | alternatives --config java There are 3 programs which provide 'java'. Selection Command ----------------------------------------------- 1 /data/jdk1.8.0_144/bin/java + 2 /data/jdk-11.0.11/bin/java * 3 /data/jdk-17.0.2/bin/java Enter to keep the current selection[+], or type selection number: [root@6791a42bf857 data]#alternatives --remove删除某个版本
[root@6791a42bf857 /]# alternatives --remove java /data/jdk-11.0.11/bin/java [root@6791a42bf857 /]# alternatives --config java There are 2 programs which provide 'java'. Selection Command ----------------------------------------------- 1 /data/jdk1.8.0_144/bin/java *+ 2 /data/jdk-17.0.2/bin/java方法2 alternatives设置环境变量
方法1的缺点:只能使用java命令,其他命令使用不了
方法2的思路:运维常用方法,软连接,其实和方法1一样
[root@6791a42bf857 /]# export JAVA_HOME=/data/java_home [root@6791a42bf857 /]# export PATH=$JAVA_HOME/bin:$PATH [root@6791a42bf857 /]# [root@6791a42bf857 /]# alternatives --install /data/java_home java /data/jdk1.8.0_144 1 [root@6791a42bf857 /]# [root@6791a42bf857 /]# java --version Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 Unrecognized option: --version Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. [root@6791a42bf857 /]# java -version Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode) [root@6791a42bf857 /]# alternatives --install /data/java_home java /data/jdk-17.0.2 2 [root@6791a42bf857 /]# java -version Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8 openjdk version "17.0.2" 2022-01-18 OpenJDK Runtime Environment (build 17.0.2+8-86) OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing) [root@6791a42bf857 /]#方法3 没有alternatives命令
自己写一个脚本,类似alternatives的功能
结合gitlab-ci.yml的default.before_script这是默认的jdk环境。https://docs.gitlab.com/ee/ci/yaml/index.html#before_script
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)