Mac下JDK安装位置查看和多版本JDK切换
作者:互联网
演示环境:
- MacBook Pro
- macOS monterey 12.2.1
JDK下载:
JDK 安装:
- dmg安装包,安装过程如一般程序。参考图形界面点击下一步默认安装即可,此处省略。
JDK安装位置查看:
不同的系统版本,位置可能存在区别
# Aker @ MacBook-Pro in ~ [13:24:51]
$ cd /Library/Java/JavaVirtualMachines/
# Aker @ MacBook-Pro in /Library/Java/JavaVirtualMachines [13:24:55]
$ ls
jdk-11.0.12.jdk jdk-17.0.1.jdk jdk1.7.0_80.jdk jdk1.8.0_172.jdk
JDK多版本切换:
可以看到,本机安装了4个版本的
jdk
,下面我将进行4个jdk
版本切换操作
- 准备好环境变量配置
## 配置jdk各个版本 ##
# 设置 JDK 7
export JAVA_7_HOME=`/usr/libexec/java_home -v 1.7`
# 设置 JDK 8
export JAVA_8_HOME=`/usr/libexec/java_home -v 1.8`
# 设置 JDK 11
export JAVA_11_HOME=`/usr/libexec/java_home -v 11`
# 设置 JDK 17
export JAVA_17_HOME=`/usr/libexec/java_home -v 17`
# 默认 JDK 8
export JAVA_HOME=$JAVA_8_HOME
#alias命令动态切换JDK版本
alias jdk7="export JAVA_HOME=$JAVA_7_HOME"
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk11="export JAVA_HOME=$JAVA_11_HOME"
alias jdk17="export JAVA_HOME=$JAVA_17_HOME"
- 将以上内容,添加到
~/.bash_profile
中
# Aker @ MacBook-Pro in ~ [13:30:29]
$ vi ~/.bash_profile
- 查看配置,执行
source
将配置生效
# Aker @ MacBook-Pro in ~ [13:30:29]
$ cat ~/.bash_profile
## 配置jdk ##
# 设置 JDK 7
export JAVA_7_HOME=`/usr/libexec/java_home -v 1.7`
# 设置 JDK 8
export JAVA_8_HOME=`/usr/libexec/java_home -v 1.8`
# 设置 JDK 11
export JAVA_11_HOME=`/usr/libexec/java_home -v 11`
# 设置 JDK 17
export JAVA_17_HOME=`/usr/libexec/java_home -v 17`
# 默认 JDK 8
export JAVA_HOME=$JAVA_8_HOME
#alias命令动态切换JDK版本
alias jdk7="export JAVA_HOME=$JAVA_7_HOME"
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
alias jdk11="export JAVA_HOME=$JAVA_11_HOME"
alias jdk17="export JAVA_HOME=$JAVA_17_HOME"
# Aker @ MacBook-Pro in ~ [13:30:29]
$ source ~/.bash_profile
- 查看效果
默认jdk为
1.8
,执行jdk7
、jdk8
、jdk11
、jdk17
等自定义别名后,会切换jdk版本
,通过java -version
可看到已切换的相应版本。
# Aker @ MacBook-Pro in ~ [13:42:05]
$ java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
# Aker @ MacBook-Pro in ~ [13:42:10]
$ jdk7
# Aker @ MacBook-Pro in ~ [13:42:23]
$ java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
# Aker @ MacBook-Pro in ~ [13:47:08]
$ jdk17
# Aker @ MacBook-Pro in ~ [13:47:15]
$ java -version
java version "17.0.1" 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
# Aker @ MacBook-Pro in ~ [13:47:18]
$ jdk11
# Aker @ MacBook-Pro in ~ [13:47:21]
$ java -version
java version "11.0.12" 2021-07-20 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.12+8-LTS-237)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.12+8-LTS-237, mixed mode)
# Aker @ MacBook-Pro in ~ [13:47:25]
$ jdk8
# Aker @ MacBook-Pro in ~ [13:47:30]
$ java -version
java version "1.8.0_172"
Java(TM) SE Runtime Environment (build 1.8.0_172-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode)
标签:JAVA,JDK,java,Mac,export,切换,HOME,MacBook 来源: https://blog.csdn.net/AllenAker/article/details/123065894