1. 进入Oracle官网JAVA的下载界面,找到自己需要下载的JDK版本。点选“Accept License Agreement”后,选择自己电脑的下载包(Mac OS X x64)并单击该包进行下载。
jdk-8u66-macosx-x64.dmg
2. 配置JAVA_HOME
装完之后发现在默认的路径下找不到JDK7的HOME,如下所示:
$ which java
/usr/bin/java
$ ls -l /usr/bin/java
lrwxr-xr-x 1 root wheel 74 10 26 22:55 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
$ ls -l /System/Library/Frameworks/JavaVM.framework/Versions
total 64
lrwxr-xr-x 1 root wheel 10 10 26 22:55 1.4 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 10 26 22:55 1.4.2 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 10 26 22:55 1.5 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 10 26 22:55 1.5.0 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 10 26 22:55 1.6 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 10 26 22:55 1.6.0 -> CurrentJDK
drwxr-xr-x 7 root wheel 238 10 26 22:55 A
lrwxr-xr-x 1 root wheel 1 10 26 22:55 Current -> A
lrwxr-xr-x 1 root wheel 52 10 26 22:55 CurrentJDK -> /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents
解决:
google了一下,发现了这篇文章Important Java Directories on Mac OS X,可以使用工具命令“/usr/libexec/java_home”来定位JAVA_HOME
vim ~/.bash_profile
加入:
JAVA_HOME=`/usr/libexec/java_home`
export JAVA_HOME
另外可以使用-V选项列出所有版本的JAVA_HOME:
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
1.8.0_66, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home
1.6.0_65-b14-468, x86_64: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home
最后不要忘记,如果.bash_profile是刚刚创建的,需要这一句让配置生效:
source .bash_profile
验证:
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_66.jdk/Contents/Home