Mac修改文件 ~/.bash_profile
# 定义不同版本jdk的java_home
export JAVA_8_HOME=/Users/maruifu/work/tools/java/jdk1.8.0_411.jdk/Contents/Home
export JAVA_17_HOME=/Users/maruifu/work/tools/java/jdk-17.0.11.jdk/Contents/Home
export JAVA_21_HOME=/Users/maruifu/work/tools/java/jdk-21.0.4.jdk/Contents/Home
# 给JAVA_HOME默认值 这个必须要有
export JAVA_HOME=$JAVA_17_HOME
# alias命令动态切换JDK版本
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk17="export JAVA_HOME=$JAVA_17_HOME"
alias jdk21="export JAVA_HOME=$JAVA_21_HOME"
PATH=$PATH:$JAVA_HOME/bin
export PATH
修改完后,执行source ~/.bash_profile
命令使改动生效
maruifu@localhost ~ % source ~/.bash_profile
maruifu@localhost ~ % java -version
java version "17.0.11" 2024-04-16 LTS
Java(TM) SE Runtime Environment (build 17.0.11 7-LTS-207)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.11 7-LTS-207, mixed mode, sharing)
maruifu@localhost ~ % jdk8
maruifu@localhost ~ % java -version
java version "1.8.0_411"
Java(TM) SE Runtime Environment (build 1.8.0_411-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.411-b09, mixed mode)
maruifu@localhost ~ % jdk21
maruifu@localhost ~ % java -version
java version "21.0.4" 2024-07-16 LTS
Java(TM) SE Runtime Environment (build 21.0.4 8-LTS-274)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.4 8-LTS-274, mixed mode, sharing)
maruifu@localhost ~ %
本文由 小马哥 创作,采用 知识共享署名4.0 国际许可协议进行许可 本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名