多个版本JDK的$JAVA_HOME环境变量在Mac OS X中设置的问题

2017-06-01

~/.profile  中增加环境变量JAVA_HOME 设置:

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/

验证变量配置:

$ source ~/.profile 
$ echo $JAVA_HOME
/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/

$ source ~/.profile 
$ echo $JAVA_HOME
/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/

显示已经配置成功了,然后执行Hadoop的启动脚本:start-dfs.sh ,启动正常后查看相关进程
ps -ef|grep hadoop,发现启动的JDK不是 $JAVA_HOME 设置的而是其他版本:

  501  7272     1   0  3:56下午 ??         0:05.28    /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home/bin/java -Dproc_namenode -Xmx1000m -Djava.net.preferIPv4Stack=true -Djava.security.krb5.realm=OX.AC.UK -Djava.security.krb5.kdc=kdc0.ox.ac.uk:kdc1.ox.ac.uk -Dhadoop.log.dir=/usr/local/share/hadoop-2.2.0/logs -Dhadoop.log.file=hadoop.log -Dhadoop.home.dir=/usr/local/share/hadoop-2.2.0 -Dhadoop.id.str=micmiu -Dhadoop.root.logger=INFO,console 

感觉非常奇怪,后来搜索了下,有人解释说是 Mac OSX新版本(10.5+)之后,已经建议 $JAVA_HOME 的设置用 /usr/libexec/java_home,因为java_home 可以根据你的需要返回指定的版本。
1、默认是查看当前版本:

$ /usr/libexec/java_home 
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

$ /usr/libexec/java_home 
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

2、-V (大写)参数可以列出所有的版本:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (5):
    1.7.0_25, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_25.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
    1.6.0_37-b06-434, x86_64:   "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home
    1.6.0_37-b06-434, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (5):
    1.7.0_25, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_25.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
    1.6.0_37-b06-434, x86_64:   "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home
    1.6.0_37-b06-434, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0_37-b06-434.jdk/Contents/Home
 
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

3、-a 参数可以根据CPU的类型过滤

$ /usr/libexec/java_home -a i386
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ /usr/libexec/java_home -a x86_64
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

$ /usr/libexec/java_home -a i386
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ /usr/libexec/java_home -a x86_64
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

4、-d 参数可以过滤 32位、64位

$ /usr/libexec/java_home -d 32
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ /usr/libexec/java_home -d 64
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home


$ /usr/libexec/java_home -d 32
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
$ /usr/libexec/java_home -d 64
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

5、-v (小写)参数可以过滤版本:

micmiu-mbp:~ micmiu$ /usr/libexec/java_home -v 1.6
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
micmiu-mbp:~ micmiu$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

micmiu-mbp:~ micmiu$ /usr/libexec/java_home -v 1.6
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
micmiu-mbp:~ micmiu$ /usr/libexec/java_home -v 1.7
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

以后就可以在 ~/.profile  中按照如下方法自定义$JAVA_HOME:

#指定默认版本
export JAVA_HOME=`/usr/libexec/java_home`
export JAVA_HOME=$(/usr/libexec/java_home)
#指定自定义版本
export JAVA_HOME=`/usr/libexec/java_home -d 64 -v 1.6`
export JAVA_HOME=$(/usr/libexec/java_home -d 64 -v 1.6)

#指定默认版本
export JAVA_HOME=`/usr/libexec/java_home`
export JAVA_HOME=$(/usr/libexec/java_home)
#指定自定义版本
export JAVA_HOME=`/usr/libexec/java_home -d 64 -v 1.6`
export JAVA_HOME=$(/usr/libexec/java_home -d 64 -v 1.6)

但是如何让Hadoop以 ~/.profile 指定的 JAVA_HOME 运行还是不知道,查看<HADOOP_HOME>/libexec/hadoop-config.sh 脚本中有关 JAVA_HOME 的逻辑代码:

# Attempt to set JAVA_HOME if it is not set
if [[ -z $JAVA_HOME ]]; then
  # On OSX use java_home (or /Library for older versions)
  if [ "Darwin" == "$(uname -s)" ]; then
    if [ -x /usr/libexec/java_home ]; then
      export JAVA_HOME=($(/usr/libexec/java_home))
    else
      export JAVA_HOME=(/Library/Java/Home)
    fi
  fi

  # Bail if we did not detect it
  if [[ -z $JAVA_HOME ]]; then
    echo "Error: JAVA_HOME is not set and could not be found." 1&gt;&amp;2
    exit 1
  fi
fi

# Attempt to set JAVA_HOME if it is not set
if [[ -z $JAVA_HOME ]]; then
  # On OSX use java_home (or /Library for older versions)
  if [ "Darwin" == "$(uname -s)" ]; then
    if [ -x /usr/libexec/java_home ]; then
      export JAVA_HOME=($(/usr/libexec/java_home))
    else
      export JAVA_HOME=(/Library/Java/Home)
    fi
  fi
 
  # Bail if we did not detect it
  if [[ -z $JAVA_HOME ]]; then
    echo "Error: JAVA_HOME is not set and could not be found." 1&gt;&amp;2
    exit 1
  fi
fi

应该是么有获取到$JAVA_HOME的值,然后执行if判断语句里面的逻辑了,目前估计只能在hadoop-env.sh中直接修改JAVA_HOME了。

Reference

环境变量在Mac OS X中设置的问题
在MAC上查找和设置$JAVA_HOME

Comments
Write a Comment