Java 1.7 on OSX 10.9.2 running as 1.5?

2020-04-07 02:38发布

Pulling my hair out about this one. java -version and javac -version both report the same 1.7.0_45, but when I try to compile the simplest class I get a class file error:

> 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

There certainly is no JDK 1.5 installed. I don't think there even is a 1.5 JDK that runs on OSX any more.

FWIW, here's the frameworks directory containing the "versions" and stub executables:

> 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

I've checked the usual suspects, there's no JAVA_HOME or CLASSPATH environment variables, not that it matters when there's no 1.5 installed. I also get the exact same behaviour if I install JDK 8.

Does anybody have an idea what's causing this?

1条回答
混吃等死
2楼-- · 2020-04-07 03:34

OK, thanks to this post (Java compilation error: Mac) I found the solution:

rm -rf ~/Library/Java/Extensions
sudo rm -rf /Library/Java/Extensions
查看更多
登录 后发表回答