在macOS中同时安装jdk8、jdk9、jdk11、jdk12并自由切换

在macOS中同时安装jdk8.9.11.12并自由切换

  • 在macOS中安装jdk,并查看是否安装成功
  • 打开根目录下的隐藏文件.bash_profile进行环境配置
  • 执行source命令生效
  • 使用jdk?实现终端命令的自由切换

在macOS中安装jdk,并查看是否安装成功

MacdeMacBook-Pro:~ mac$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
    12.0.2, x86_64:	"Java SE 12.0.2"	/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
    11.0.4, x86_64:	"Java SE 11.0.4"	/Library/Java/JavaVirtualMachines/jdk-11.0.4.jdk/Contents/Home
    9.0.4, x86_64:	"Java SE 9.0.4"	/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
    1.8.0_221, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home

打开根目录下的隐藏文件.bash_profile进行环境配置

打开.bash_profile,没有的话创建
写入以下内容

# added by Anaconda3 5.2.0 installerexport JAVA_8_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
export JAVA_9_HOME=/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
export JAVA_11_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.4.jdk/Contents/Home
export JAVA_12_HOME=/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
alias jdk8="export JAVA_HOME=$JAVA_8_HOME" #编辑一个命令jdk8,输入则转至jdk1.8
alias jdk9="export JAVA_HOME=$JAVA_9_HOME" #编辑一个命令jdk9,输入则转至jdk1.9
alias jdk11="export JAVA_HOME=$JAVA_11_HOME" #编辑一个命令jdk11,输入则转至jdk1.11
alias jdk12="export JAVA_HOME=$JAVA_12_HOME" #编辑一个命令jdk12,输入则转至jdk1.12
export JAVA_HOME=`/usr/libexec/java_home`  #最后安装的版本,这样当自动更新时,始终指向最新版本

执行source命令生效

MacdeMacBook-Pro:~ mac$ source .bash_profile

使用jdk?实现终端命令的自由切换

MacdeMacBook-Pro:~ mac$ jdk9
MacdeMacBook-Pro:~ mac$ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
MacdeMacBook-Pro:~ mac$ jdk11
MacdeMacBook-Pro:~ mac$ java -version
java version "11.0.4" 2019-07-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.4+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.4+10-LTS, mixed mode)
MacdeMacBook-Pro:~ mac$ jdk8
MacdeMacBook-Pro:~ mac$ java -version
java version "1.8.0_221"
Java(TM) SE Runtime Environment (build 1.8.0_221-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode)
MacdeMacBook-Pro:~ mac$ jdk12
MacdeMacBook-Pro:~ mac$ java -version
java version "12.0.2" 2019-07-16
Java(TM) SE Runtime Environment (build 12.0.2+10)
Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing)

你可能感兴趣的:(jdk安装已经环境配置)