错误: 找不到或无法加载主类 org.codehaus.plexus.classworlds.launcher.Launcher 问题排查

1. mac 下面是用homebrew 安装maven,安装成功,设置完M2_HOME ,使用mvn -version进行检测

houenxundeMacBook-Pro:3.3.9 houenxun$ mvn -version
错误: 找不到或无法加载主类 org.codehaus.plexus.classworlds.launcher.Launcher

2. 网上看了一堆解决方案,不明所以,于是自己尝试解决,首先,先定位一下问题的脚本

houenxundeMacBook-Pro:3.3.9 houenxun$ which mvn
/usr/local/bin/mvn
houenxundeMacBook-Pro:3.3.9 houenxun$ ls -l /usr/local/bin/mvn
lrwxr-xr-x  1 houenxun  admin  29  4  7 15:52 /usr/local/bin/mvn -> ../Cellar/maven/3.3.9/bin/mvn

打开mvn文件

#!/bin/bash
JAVA_HOME="${JAVA_HOME:-$(/usr/libexec/java_home)}" exec "/usr/local/Cellar/maven/3.3.9/libexec/bin/mvn" "$@"

可以看到真正调用的是

/usr/local/Cellar/maven/3.3.9/libexec/bin/mvn

打开该文件,文件比较长,定位到最后报错的地方

exec "$JAVACMD" \
  $MAVEN_OPTS \
  $MAVEN_DEBUG_OPTS \
  -classpath "${M2_HOME}"/boot/plexus-classworlds-*.jar \
  "-Dclassworlds.conf=${M2_HOME}/bin/m2.conf" \
  "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
  ${CLASSWORLDS_LAUNCHER} "$@"

使用echo打印真正的语句

exec /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/java -classpath /usr/local/Cellar/maven/3.3.9/boot/plexus-classworlds-*.jar -Dclassworlds.conf=/usr/local/Cellar/maven/3.3.9/bin/m2.conf -Dmaven.home=/usr/local/Cellar/maven/3.3.9 -Dmaven.multiModuleProjectDirectory=/usr/local/Cellar/maven/3.3.9/libexec/bin org.codehaus.plexus.classworlds.launcher.Launcher

3. 很奇怪,在/usr/local/Cellar/maven/3.3.9目录下boot和bin都是不存在的,而却存在/usr/local/Cellar/maven/3.3.9/libexec目录下

houenxundeMacBook-Pro:3.3.9 houenxun$ pwd
/usr/local/Cellar/maven/3.3.9
houenxundeMacBook-Pro:3.3.9 houenxun$ ls
INSTALL_RECEIPT.json	NOTICE			bin
LICENSE			README.txt		libexec
houenxundeMacBook-Pro:3.3.9 houenxun$
houenxundeMacBook-Pro:3.3.9 houenxun$ cd libexec/
houenxundeMacBook-Pro:libexec houenxun$ ls
bin	boot	conf	lib
houenxundeMacBook-Pro:libexec houenxun$

4. 尝试将他们拷贝出去,的确OK了

houenxundeMacBook-Pro:3.3.9 houenxun$ mvn -version
exec /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/java -classpath /usr/local/Cellar/maven/3.3.9/boot/plexus-classworlds-2.5.2.jar -Dclassworlds.conf=/usr/local/Cellar/maven/3.3.9/bin/m2.conf -Dmaven.home=/usr/local/Cellar/maven/3.3.9 -Dmaven.multiModuleProjectDirectory=/usr/local/Cellar/maven/3.3.9 org.codehaus.plexus.classworlds.launcher.Launcher -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/Cellar/maven/3.3.9
Java version: 1.8.0_77, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.4", arch: "x86_64", family: "mac"

5. 问题似乎解决了,但不禁奇怪,难道homebrew安装maven有bug???

我习惯性的设置M2_HOME,为啥要设置,是要把maven的目录设置成执行环节目录,但根据第二步骤

/usr/bin/mvn是mvn命令的符号链接,有了这个符号链接M2_HOME也就不起作用了。我们尝试禁用M2_HOME环境变量

houenxundeMacBook-Pro:~ houenxun$ mvn -version
exec /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/java -classpath /usr/local/Cellar/maven/3.3.9/libexec/boot/plexus-classworlds-2.5.2.jar -Dclassworlds.conf=/usr/local/Cellar/maven/3.3.9/libexec/bin/m2.conf -Dmaven.home=/usr/local/Cellar/maven/3.3.9/libexec -Dmaven.multiModuleProjectDirectory=/Users/houenxun org.codehaus.plexus.classworlds.launcher.Launcher -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_77, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.4", arch: "x86_64", family: "mac"

依然可以执行,而且boot和lib的路径也发生变化了!

原来新版本的maven已经不需要配置M2_HOME 环境变量,并且目录层次结构也发生了变化,但是为了兼容原来的老版本,脚本中通过M2_HOME来选择新旧目录层次,哎,自己画蛇添足了!!!

你可能感兴趣的:(maven,mac,org)