Mac 安装多个jdk踩过的坑

我是先安装了jdk 8,然后安装了jdk7,通过命令可以看到,默认链接的是jdk8

bianxh:Versions bianxh$ which java
/usr/bin/java
bianxh:Versions bianxh$ ls -l /usr/bin/java
lrwxr-xr-x  1 root  wheel  74  9 22  2016 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
bianxh:Versions bianxh$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home
bianxh:Versions bianxh$ java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

配置~/.bash_profile

export PATH=${PATH}:/Users/bianxh/Library/Android/sdk//platform-tools
export PATH=${PATH}:/Users/bianxh/Library/Android/sdk//tools
export PATH=${PATH}:/usr/local/apache-tomcat-7.0.77/bin
export JAMES_SRC_HOME=/usr/local/james-project/james-project
# 设置 JDK 8
export JAVA_8_HOME=$(/usr/libexec/java_home -v 1.8)
# 设置 JDK 7
export JAVA_7_HOME=$(/usr/libexec/java_home -v 1.7)
export JAVA_HOME=$JAVA_7_HOME
#alias命令动态切换JDK版本
alias jdk7="export JAVA_HOME=$"JAVA_7_HOME"
alias jdk8="export JAVA_HOME=$"JAVA_8_HOME"
alias subl="'/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl'"
alias ll='ls -al'

使用命令生效配置:

bianxh:Versions bianxh$ source ~/.bash_profile 

现在查看:

bianxh:Versions bianxh$ 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)
bianxh:Versions bianxh$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
bianxh:Versions bianxh$ /usr/libexec/java_home -v 1.8
/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home

以后就可以通过命令自由切换了,效果如下:

bianxh:libexec bianxh$ jdk8
bianxh:libexec bianxh$ java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
bianxh:libexec bianxh$ jdk7
bianxh:libexec bianxh$ 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)

当然,如果用eclipse,注意修改JAVA_HOME,在~/.bash_profile中修改。

你可能感兴趣的:(Mac 安装多个jdk踩过的坑)