欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

linux环境动态切换jdk版本

时间:2023-08-12
linux环境动态切换jdk版本

很多时候我们需要不同的jdk环境,那就会有一个问题。如何动态切换jdk版本呢?

方法1 alternatives命令

nodejs有nvm,但是jdk没有这样的工具。 但是没想到linux系统本身就带了一个类似版本切换的工具。

使用步骤

解压jdk的安装包到同一个目录下,如

[root@6791a42bf857 data]# lltotal 16drwxr-xr-x 8 root root 4096 Jan 25 11:26 jdk-11.0.11drwxr-xr-x 8 root root 4096 Jan 25 11:27 jdk-17.0.2drwxr-xr-x 8 10 143 4096 Jul 22 2017 jdk1.8.0_144[root@6791a42bf857 data]#[root@6791a42bf857 data]# alternatives --helpalternatives 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 --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]#

alternatives --install安装版本

[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 javaThere 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 javaThere 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

alternatives --config配置版本

[root@6791a42bf857 data]# alternatives --config javaThere 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/javaEnter to keep the current selection[+], or type selection number: 3[root@6791a42bf857 data]# alternatives --config javaThere 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/javaEnter to keep the current selection[+], or type selection number:[root@6791a42bf857 data]# java -versionPicked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8openjdk version "17.0.2" 2022-01-18OpenJDK 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 javajava - 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 3Current `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 javajava - 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 3Current `best' version is /data/jdk-17.0.2/bin/java.[root@6791a42bf857 data]#[root@6791a42bf857 data]# echo 3 | alternatives --config javaThere 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/javaEnter 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 javaThere 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 --versionPicked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8Unrecognized option: --versionError: Could not create the Java Virtual Machine.Error: A fatal exception has occurred、Program will exit.[root@6791a42bf857 /]# java -versionPicked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8java 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 -versionPicked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8openjdk version "17.0.2" 2022-01-18OpenJDK 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

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。