我最近在我的 Macbook Pro 上升级到 Java7。我下载了 JDK(不是 JRE)。
» javac version
javac 1.7.0_17
» echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home
但是,在尝试运行构建时,其中一个 Maven 编译器插件失败,声称我安装了 JRE:
» mvn install
[ERROR] execute error
org.apache.maven.plugin.MojoExecutionException: You need to run build with JDK
or have tools.jar on the classpath.
If this occures during eclipse build make sure you run eclipse under JDK as well
at com.mysema.maven.apt.AbstractProcessorMojo.execute(AbstractProcessorMojo.java:263)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
我很困惑,因为我显然安装了 JDK。我MAVEN_OPTS
没有做任何有趣的事情:
» echo $MAVEN_OPTS
-Xmx512m
尝试调试时,我检查了有问题的插件的来源,它正在执行以下操作:
try {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null) {
throw new MojoExecutionException("You need to run build with JDK or have tools.jar on the classpath."
+ "If this occures during eclipse build make sure you run eclipse under JDK as well");
}
这似乎是无害的,所以我怀疑我的命令行环境有问题,并写了一个简单的测试:
// Main.java
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
public class Main {
public static void main(String[] args) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null)
{
System.out.print("Compiler is null");
} else {
System.out.print("Compiler is not null");
}
}
}
» javac Main.java
Main.java:1: cannot access javax.tools.JavaCompiler
bad class file: /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/rt.jar(javax/tools/JavaCompiler.class)
class file has wrong version 51.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
import javax.tools.JavaCompiler;
^
1 error
如果我正确理解该错误,它表明 Java7 编译器针对 Java5 运行rt.jar
?
我不确定这里发生了什么。
我显然安装了 Java7 JDK,但不明白:
- 为什么 maven 插件为编译器返回 null?
- 为什么我会收到类版本错误?
更新
答案表明 JRE / JDK 安装混乱。我同意这似乎很可能,但无法追踪罪魁祸首所在。
一些附加信息:
» which javac
/usr/bin/javac
ls -ltra /usr/bin/javac
/usr/bin/javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac
cd /System/Library/Frameworks/JavaVM.framework/Versions
ls -ltra
lrwxr-xr-x 1 root wheel 10 18 Feb 08:36 1.6.0 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 18 Feb 08:36 1.6 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 18 Feb 08:36 1.5.0 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 18 Feb 08:36 1.5 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 18 Feb 08:36 1.4.2 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 18 Feb 08:36 1.4 -> CurrentJDK
drwxr-xr-x 8 root wheel 272 18 Feb 08:39 A
drwxr-xr-x 11 root wheel 374 18 Feb 08:39 ..
lrwxr-xr-x 1 root wheel 1 14 Jun 11:14 Current -> A
lrwxr-xr-x 1 root wheel 58 14 Jun 11:15 CurrentJDK -> /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents
drwxr-xr-x 11 root wheel 374 14 Jun 11:15 .
因此,正在运行的 javac 是安装在A/Commands
. 我不确定这是否正确。从我的 Windows 时代开始,这似乎是错误的,但我对 Mac 如何处理 Java 安装进行修补还不够熟悉。
我的$JAVA_HOME
观点/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home
,其中有以下内容:
drwxrwxr-x 10 root wheel 340 5 Feb 08:10 jre
-rw-rw-r-- 1 root wheel 123324 5 Feb 08:10 THIRDPARTYLICENSEREADME-JAVAFX.txt
drwxrwxr-x 5 root wheel 170 5 Feb 08:10 man
drwxrwxr-x 9 root wheel 306 2 Mar 02:10 db
-rw-rw-r-- 1 root wheel 3339 2 Mar 02:10 COPYRIGHT
drwxrwxr-x 9 root wheel 306 2 Mar 02:10 include
-rw-rw-r-- 1 root wheel 19997030 2 Mar 02:10 src.zip
-rw-rw-r-- 1 root wheel 447 2 Mar 02:10 release
-rw-rw-r-- 1 root wheel 172252 2 Mar 02:10 THIRDPARTYLICENSEREADME.txt
-rw-rw-r-- 1 root wheel 114 2 Mar 02:10 README.html
-rw-rw-r-- 1 root wheel 40 2 Mar 02:10 LICENSE
drwxrwxr-x 5 root wheel 170 2 Mar 02:10 ..
drwxrwxr-x 15 root wheel 510 2 Mar 02:13 .
drwxrwxr-x 13 root wheel 442 2 Mar 02:13 lib
drwxrwxr-x 43 root wheel 1462 2 Mar 02:13 bin