mac系统快速切换不同版本JDK

  • 1.安装所需jdk版本

下载地址:http://www.codebaoku.com/jdk/jdk-index.html

mac系统快速切换不同版本JDK_第1张图片
本示例安装了jdk8和jdk19两个版本

  • 2.查看对应安装路径

安装好后,通过终端输入以下命令查看相关路径(后续需在.bash_profile中配置)

/usr/libexec/java_home -V
Matching Java Virtual Machines (3):
    19 (x86_64) "Oracle Corporation" - "Java SE 19" /Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home
    1.8.361.09 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_361 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_361.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home

记录对应的jdk路径
JDK1.8版本:
/Library/Java/JavaVirtualMachines/jdk1.8.0_361.jdk/Contents/Home/legal/jdk
JDK19版本:
/Library/Java/JavaVirtualMachines/jdk-19.jdk/Contents/Home

  • 3.信息配置
 sudo vi ~/.bash_profile

增加配置如下:

JAVA_HOME_8=/Library/Java/JavaVirtualMachines/jdk1.8.0_361.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_8
export JRE_HOME
export PATH
export CLASSPATH

alias jdk8="export JAVA_HOME=$JAVA_HOME_8"
alias jdk19="export JAVA_HOME=$JAVA_HOME_19"

执行source生效配置文件

source ~/.bash_profile
  • 4.效果验证

通过jdk19或者jdk8来切换

MacBook-Pro Home % jdk8
MacBook-Pro Home % java -version
java version "1.8.0_361"
Java(TM) SE Runtime Environment (build 1.8.0_361-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.361-b09, mixed mode)
MacBook-Pro Home % jdk19         
MacBook-Pro Home % java -version
java version "19" 2022-09-20
Java(TM) SE Runtime Environment (build 19+36-2238)
Java HotSpot(TM) 64-Bit Server VM (build 19+36-2238, mixed mode, sharing)

至此配置全部完成,赶快开始愉快的编程吧!!

-----编写者:梦想的边缘

你可能感兴趣的:(jdk)