记一次在mac上配置Maven 的JAVA_HOME的惨痛经历

今天装好了了Maven3.3.3后,死活运行不了

CvdeMacBook-Pro:~ chenwei$ mvn
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0

这个错误明显是运行的jdk版本低于编译的jdk版本,但是本地的jdk已经装成1.8了,maven3.3.3只需要1.7而已

CvdeMacBook-Pro:~ chenwei$ java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

配置了JAVA_HOME依然没用(`/usr/libexec/java_home`意思是执行``中的内容替换,/usr/libexec/java_home是mac推荐的设置方式)

CvdeMacBook-Pro:apache-maven-3.3.3 chenwei$ cat ~/.bash_profile

# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
M3_HOME="/Users/chenwei/Developers/Maven/apache-maven-3.3.3"
JAVA_HOME="`/usr/libexec/java_home`"
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
PATH="$JAVA_HOME/bin:$M3_HOME/bin:$PATH"

export PATH

然后我查看mvn里面找JAVA_HOME的方式

bash-3.2$ vi `which mvn`

在里面找到加载方式

          #
           # Look for the Apple JDKs first to preserve the existing behaviour, and then look
           # for the new JDKs provided by Oracle.
           #
           if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ] ; then
             #
             # Apple JDKs
             #
             export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
           fi

           if [ -z "$JAVA_HOME" ] && [ -L /System/Library/Java/JavaVirtualMachines/CurrentJDK ] ; then
             #
             # Apple JDKs
             #
             export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
           fi

           if [ -z "$JAVA_HOME" ] && [ -L "/Library/Java/JavaVirtualMachines/CurrentJDK" ] ; then
             #
             # Oracle JDKs
             #
             export JAVA_HOME=/Library/Java/JavaVirtualMachines/CurrentJDK/Contents/Home
           fi

           if [ -z "$JAVA_HOME" ] && [ -x "/usr/libexec/java_home" ]; then
             #
             # Apple JDKs
             #
             export JAVA_HOME=`/usr/libexec/java_home`
           fi

我想我已经配置过JAVA_HOME了不可能有问题啊

调试看看

bash-3.2$ sh -x mvn
+ '[' -z '' ']'
+ '[' -f /etc/mavenrc ']'
+ '[' -f /Users/chenwei/.mavenrc ']'
+ cygwin=false
+ darwin=false
+ mingw=false
+ case "`uname`" in
++ uname
+ darwin=true
+ '[' -z '' ']'
+ '[' -L /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK ']'
+ export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home
+ JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home

这里发现没有找到JAVA_HOME,于是设置成jdk1.6了

由于我坚信我设置了~/.bash.profile 的JAVA_HOME.

echo $JAVA_HOME是有值的

然后看到mvn开头设置的#!/bin/sh,怀疑是sh找不到JAVA_HOME

试了一下

bash-3.2$ sh
sh-3.2$ echo $JAVA_HOME

sh-3.2$

果然如此,于是开始研究怎么设置sh的环境变量,把/etc/profile中的JAVA_HOME也按.bash_profile的方式设置了一遍

还是没用

然后开始研究shell的环境变量加载,看到erport的作用时,我终于反应过来,我设置了JAVA_HOME,但是没有export,只export了PATH.

于是修改.bash_profile

sh-3.2$ vi ~/.bash_profile
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
M3_HOME="/Users/chenwei/Developers/Maven/apache-maven-3.3.3"
JAVA_HOME="`/usr/libexec/java_home`"
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
PATH="$JAVA_HOME/bin:$M3_HOME/bin:$PATH"
export JAVA_HOME
export M3_HOME
export PATH

source一下,然后再运行mvn,终于搞定了

CvdeMacBook-Pro:~ chenwei$ mvn -v
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)
Maven home: /Users/chenwei/Developers/Maven/apache-maven-3.3.3
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.10.4", arch: "x86_64", family: "mac"

这种低级错误,都是因为不熟悉linux导致的,简直笨得哭出来

你可能感兴趣的:(JAVA_HOME)