在MAC上查找和设置$JAVA_HOME
最近升级了MAC OS,装了JDK7 for mac,在这里下载JDK7 for mac,装完之后发现在默认的路径下找不到JDK7的HOME,如下所示:
- $ 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
- $ ls -l /System/Library/Frameworks/JavaVM.framework/Versions
- total 64
- lrwxr-xr-x 1 root wheel 10 Jan 4 20:24 1.4 -> CurrentJDK
- lrwxr-xr-x 1 root wheel 10 Jan 4 20:24 1.4.2 -> CurrentJDK
- lrwxr-xr-x 1 root wheel 10 Jan 4 20:24 1.5 -> CurrentJDK
- lrwxr-xr-x 1 root wheel 10 Jan 4 20:24 1.5.0 -> CurrentJDK
- lrwxr-xr-x 1 root wheel 10 Jan 4 20:24 1.6 -> CurrentJDK
- lrwxr-xr-x 1 root wheel 10 Jan 4 20:24 1.6.0 -> CurrentJDK
- drwxr-xr-x 7 root wheel 238 Jan 4 20:25 A
- lrwxr-xr-x 1 root wheel 1 Jan 4 20:24 Current -> A
- lrwxr-xr-x 1 root wheel 59 Jan 4 20:24 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents
可是查看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,这下爽了:
- $ 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:
- $ /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