4
votes

I had a compilation problem with Eclipse with 1.7 JDK installed running on Mavericks:

class file has wrong version 51.0, should be 49.0

So, as this looked like a javac versioning problem I removed all java versions. Installed 1.7_51, installed the Apple supplied 1.6 version so that Eclipse would run, changed sym links so 1.7 was the default. Same problem as before, the same project would not compile, though others did,

Next step, strip the problem down to the bare bones i.e. remove ant etc from the equation - Test.java file used has no methods, just a main with System.out.println(). cd to the directory containing the source file and ran the compiler specifying full path to javac, having checked that JAVA_HOME was not set:

$ /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/javac Test.java -verbose -extdirs . -endorseddirs . -cp . -bootclasspath /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/rt.jar -version

Output:

javac 1.7.0_51

[parsing started Test.java]

[parsing completed 67ms]

[search path for source files: [.]]

[search path for class files:[/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/rt.jar, .]]

[loading /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.class)]

Test.java:11: cannot access java.lang.Object bad class file: /Library/Java/JavaVirtualMachines/jdk1.7.0_51.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 Test { ^

[total 336ms] 1 error

Flumoxed.

Any ideas ?

On a related note I seem to be unable to get javac to accept a -target option above 1.5/5:

$ /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/javac Test.java -verbose -extdirs . -endorseddirs . -cp . -bootclasspath /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/rt.jar -version -target 1.7

results in:

javac 1.7.0_51

javac: invalid target release: 1.7

1
Try updating the JRE you're using and or looking at the versions, if you have some files that are still existing from an ancient version of your JDK.Ferdz
I removed everything then added back 1.7_51 JDK, then the OSX 1.6 JRE (Eclipse has issues if you don't have this). java -version gives Java(TM) SE Runtime Environment (build 1.7.0_51-b13) and by specifying where to look for everything on the command line the javac call should be pretty isolated from anything out of date.user3439760
@user4349760 And does it still throws the same error?Ferdz
Yes it does. Sanity checked that the command line was run as asked using ps. It is.user3439760

1 Answers

8
votes

After much playing around with the numerous discussions fixes posted - all of which didn't do the trick - I resorted to using dtrace to see what files were being accessed by javac when the command was run.

In terminal (you need XCode installed)

sudo dtrace -s /dev/stdin

syscall::open*:entry

{

   printf("%s %s", execname, copyinstr(arg0));

}

<Ctrl-D>

The running the command line in another terminal instance I found buried in the file accessing activity for javac - which is pretty extensive considering the command line options give you the impression that you are restricting where it looks for files - I found

open:entry javac /Users/me/Library/Java/Extensions/tools.jar

I didn't realise I had a local extensions folder.....

Deleting the contents of it fixed the problem.

So, it is a version mismatch caused by old jars - as others have discovered - the problem in my case was finding them