6

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?

Sophistifunk
  • 4,742
  • 4
  • 28
  • 37
  • What does `which java` give you? – Justin Jasmann Mar 27 '14 at 22:54
  • which java gives me the same thing it always does on OSX, the stub executable in /System/Frameworks/Java/whatever. Dtrace also tells me it's eventually forking the correct executable in the jdk install – Sophistifunk Mar 27 '14 at 23:02
  • Where are you executing the JVM on some IDE or on command line – Puru-- Mar 27 '14 at 23:12
  • Running from the command line. I also get internal javac errors attempting to compile from Idea. – Sophistifunk Mar 27 '14 at 23:21
  • What about `/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home` 's output? I'll agree it's super weird javac seems to be 1.5, though I've got 1.5 versions installed here: `/System/Library/Frameworks/JavaVM.framework/Versions` – Justin Jasmann Mar 27 '14 at 23:34
  • java_home util reports the correct value. Also you'll probably find that you don't actually have a 1.5 installed, just a symlink for backwards compatibility with the way things were a few years ago with Apple's JVMs. – Sophistifunk Mar 28 '14 at 00:06
  • i'm having a similar problem. my /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home‌​ reports 1.7 when I've updated to 1.8 – user1126515 Sep 28 '15 at 15:54

1 Answers1

5

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

rm -rf ~/Library/Java/Extensions
sudo rm -rf /Library/Java/Extensions
Community
  • 1
  • 1
Sophistifunk
  • 4,742
  • 4
  • 28
  • 37