把头发拉出来。 java -version
和javac -version
都报告相同的1.7.0_45,但是当我尝试编译最简单的类时,我得到一个类文件错误:
> cat A.java
public class A {}
> javac A.java
A.java:1: cannot access java.lang.Object
bad class file: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.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.
public class A {}
^
1 error
肯定没有安装JDK 1.5。我不认为甚至 是一个在OSX上运行的1.5 JDK。
FWIW,这里是包含"版本"的框架目录。和存根可执行文件:
> ls -al /System/Library/Frameworks/JavaVM.framework/Versions/
total 64
drwxr-xr-x 11 root wheel 374 28 Mar 09:03 .
drwxr-xr-x 12 root wheel 408 11 Mar 07:52 ..
lrwxr-xr-x 1 root wheel 10 29 Oct 12:54 1.4 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 29 Oct 12:54 1.4.2 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 29 Oct 12:54 1.5 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 29 Oct 12:54 1.5.0 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 29 Oct 12:54 1.6 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 29 Oct 12:54 1.6.0 -> CurrentJDK
drwxr-xr-x 8 root wheel 272 28 Mar 09:03 A
lrwxr-xr-x 1 root wheel 1 29 Oct 12:54 Current -> A
lrwxr-xr-x 1 root wheel 1 28 Mar 09:03 CurrentJDK -> A
我检查了通常的嫌疑人,没有JAVA_HOME或CLASSPATH环境变量,并不重要,因为没有安装1.5。如果我安装JDK 8,我也会得到完全相同的行为。
有人知道造成这种情况的原因是什么吗?
答案 0 :(得分:5)
好的,感谢这篇文章(Java compilation error: Mac)我找到了解决方案:
rm -rf ~/Library/Java/Extensions
sudo rm -rf /Library/Java/Extensions