Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
941 views
in Technique[技术] by (71.8m points)

macos - Java compilation error: Mac

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...