首先輸入命令:vi .bash_profile ,添加以下內容: java
# Mac默認 JDK 6(Mac默認自帶了一個jdk6版本) export JAVA_6_HOME=`/usr/libexec/java_home -v 1.6` # 設置 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 6 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生效新配置bash
很是方便,效果以下:ui
Last login: Thu Feb 4 20:51:26 on ttys002 liqiu ~$ java -version java version "1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-468-11M4833) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-468, 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) 64-Bit Server VM (build 24.79-b02, mixed mode) liqiu ~$