在Linux切换JDK一条命令就可以接口
代码语言:javascript复制update-alternatives --config java
案例:
[root@VM-4-14-centos lib]# update-alternatives --config java
共有 2 个提供“java”的程序。
选项 命令
-----------------------------------------------
* 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b08-1.el7_9.x86_64/jre/bin/java)
2 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.18.0.10-1.el7_9.x86_64/bin/java)
按 Enter 保留当前选项[ ],或者键入选项编号:2
但是在Mac中可没有。我们本次计划:配别名的方式实现多JDK版本切换,比如JDK11就是java11 -version,JDK17就是java17 -version
编辑
代码语言:javascript复制vi ~/.bash_profile
代码语言:javascript复制JAVA_HOME_8=/Library/Java/JavaVirtualMachines/jdk-8.jdk/Contents/Home
JAVA_HOME_11=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
JAVA_HOME_17=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
JAVA_HOME_19=/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export JAVA_HOME=$JAVA_HOME_17
export JRE_HOME
export PATH
export CLASSPATH
alias jdk8="export JAVA_HOME=$JAVA_HOME_8"
alias jdk11="export JAVA_HOME=$JAVA_HOME_11"
alias jdk17="export JAVA_HOME=$JAVA_HOME_17"
alias jdk19="export JAVA_HOME=$JAVA_HOME_19"
使得配置生效
代码语言:javascript复制source ~/.bash_profile
测试使用
先输入jdkX后,再使用java命令即可!
代码语言:javascript复制zanglikun@zanglikundeMacBook-Pro lib % jdk11
zanglikun@zanglikundeMacBook-Pro lib % java -version
java version "11.0.18" 2023-01-17 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.18 9-LTS-195)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.18 9-LTS-195, mixed mode)
zanglikun@zanglikundeMacBook-Pro lib % jdk17
zanglikun@zanglikundeMacBook-Pro lib % java -version
java version "17.0.6" 2023-01-17 LTS
Java(TM) SE Runtime Environment (build 17.0.6 9-LTS-190)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.6 9-LTS-190, mixed mode, sharing)
特殊说明: 以上文章,均是我实际操作,写出来的笔记资料,不会盗用别人文章!烦请各位,请勿直接盗用!转载记得标注来源!