文章来源:http://guibin.iteye.com/blog/1999238
最近升级了MAC OS,装了JDK7 for mac,在这里下载JDK7 for mac,装完之后发现在默认的路径下找不到JDK7的HOME,如下所示:
shell 代码:
$ which java
/usr/bin/java
$ ls -l /usr/bin/java
lrwxr-xr-x 1 root wheel 74 Jan 4 20:24 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
可是查看java的版本确实是1.7,如下:
$ java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)
google了一下,发现了这篇文章Important Java Directories on Mac OS X,可以使用工具命令“/usr/libexec/java_home”来定位JAVA_HOME,这下爽了:
Shell代码
输入/usr/libexec/java_home
得到/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
以下命令在我的mac上没有打印出来,不过JAVA_HOME成功了
$ cat ~/.profile
JAVA_HOME=`/usr/libexec/java_home`
export JAVA_HOME
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
另外可以使用-V选项列出所有版本的JAVA_HOME:
Shell代码
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (3):
1.7.0_45, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home