首先输入命令:vi .bash_profile ,添加如下内容:
# Mac默认 JDK (Mac默认自带了一个jdk6版本)
export JAVA_6_HOME=`/usr/libexec/java_home -v 1.6`
# 设置 JDK
export JAVA_7_HOME=`/usr/libexec/java_home -v 1.7`
# 设置 JDK
export JAVA_8_HOME=`/usr/libexec/java_home -v 1.8` #默认JDK
export JAVA_HOME=$JAVA_6_HOME #alias命令动态切换JDK版本
alias jdk6="export JAVA_HOME=$JAVA_6_HOME"
alias jdk7="export JAVA_HOME=$JAVA_7_HOME"
alias jdk8="export JAVA_HOME=$JAVA_8_HOME"
然后执行source .bash_profile生效新配置
非常方便,效果如下:
Last login: Thu Feb :: on ttys002
liqiu ~$ java -version
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14--11M4833)
Java HotSpot(TM) -Bit Server VM (build 20.65-b04-, mixed mode)
liqiu ~$ jdk7
liqiu ~$ java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) -Bit Server VM (build 24.79-b02, mixed mode)
liqiu ~$