Mac下配置JAVA_HOME
http://blog.csdn.net/shallowgrave/article/details/39367119
闲来无事,装个Hbase玩玩,突然发现Mac下默认安装的JDK7,没有配置JAVA_HOME没有那么就自己配呗。
打开之前配置Hadoop集群时配置Java环境的笔记,找到添加环境变量这一步:
- export JAVA_HOME=/usr/java/latest
- export CLASSPATH=.:$JAVA_HOME
- export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/binexport CLASSPATH=.:$JAVA_HOME/lib:$CLASSPATH
之前是linux环境下配置的,现在换到另一个类Unix系统MacOS下来,想来也是类似的。只要找到JDK的真实安装路径即可。
刚好之前在配置maven依赖的JDK依赖有点类似经验。
- shuchen-2:bin seishuchen$ which java
- /usr/bin/java
- shuchen-2:bin seishuchen$ ls -la /usr/bin/java
- lrwxr-xr-x 1 root wheel 74 Mar 4 2014 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
- shuchen-2:bin seishuchen$
很快找到了当前JDK的真实路径。查看路径名发现,有Versions字段还有Current字段,作为一个程序猿,这点敏感还是有的。
马上切换到Versions目录下面,查看:
- shuchen-2:Versions seishuchen$ cd /System/Library/Frameworks/JavaVM.framework/Versions
- shuchen-2:Versions seishuchen$ ls -al
- total 72
- drwxr-xr-x 12 root wheel 408 Jul 15 17:13 .
- drwxr-xr-x 12 root wheel 408 Apr 11 15:10 ..
- lrwxr-xr-x 1 root wheel 10 Mar 4 2014 1.4 -> CurrentJDK
- lrwxr-xr-x 1 root wheel 10 Mar 4 2014 1.4.2 -> CurrentJDK
- lrwxr-xr-x 1 root wheel 10 Mar 4 2014 1.5 -> CurrentJDK
- lrwxr-xr-x 1 root wheel 10 Mar 4 2014 1.5.0 -> CurrentJDK
- lrwxr-xr-x 1 root wheel 10 Mar 4 2014 1.6 -> CurrentJDK
- lrwxr-xr-x 1 root wheel 10 Mar 4 2014 1.6.0 -> CurrentJDK
- drwxr-xr-x 8 root wheel 272 Mar 4 2014 A
- lrwxr-xr-x 1 root wheel 1 Mar 4 2014 Current -> A
- lrwxr-xr-x 1 root wheel 58 Jul 15 17:12 CurrentJDK -> /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents
- lrwxr-xr-x 1 root wheel 59 Mar 4 2014 CurrentJDK_BAK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents
结果很明显,有木有,原来都指向了/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents
那么就切换过去瞅瞅呗。
- shuchen-2:Versions seishuchen$ cd /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents
- shuchen-2:Contents seishuchen$ ls
- Home Info.plist MacOS
- shuchen-2:Contents seishuchen$ cd Home/
- shuchen-2:Home seishuchen$ ls
- COPYRIGHT bin man
- LICENSE db release
- README.html include src.zip
- THIRDPARTYLICENSEREADME-JAVAFX.txt jre
- THIRDPARTYLICENSEREADME.txt lib
- shuchen-2:Home seishuchen$
看到了久违的JDK安装目录了不是。好了,我们的JAVA_HOME即是/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home。
过程看着挺复杂的样子,当然这个是对于一个最原始的方法,如果你熟悉Mac的话,其实有个获得JAVA_HOME的自带小工具,直接在命令行
输入/usr/libexec/java_home来获得。
- shuchen-2:Home seishuchen$ /usr/libexec/java_home
- /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home
- shuchen-2:Home seishuchen$
那么接下来只要修改配置文件使之生效即可。根据你的需求,可以选择修改不同的环境变量配置文件。
而笔者只是修改当前用户的即可。只要在~/.profile(mac下也可以编辑~/.bashrc,但~/.bash_profile就不确定了,感兴趣的可以自己事先)
加入:
- JAVA_HOME=`/usr/libexec/java_home`
- export JAVA_HOME
- export CLASSPATH=.:$JAVA_HOME
- export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
- export CLASSPATH=.:$JAVA_HOME/lib:$CLASSPATH
接着source ~/.profile ,好了,收工。
执行Hbase启动脚本,用jps查看一下情况,发现一切是那么完美,哦哈哈