简体   繁体   English

Java编译错误:Mac

[英]Java compilation error: Mac

I had a compilation problem with Eclipse with 1.7 JDK installed running on Mavericks: 我在Eclipse上运行1.7 JDK时出现了编译问题:

class file has wrong version 51.0, should be 49.0 类文件有错误的版本51.0,应该是49.0

So, as this looked like a javac versioning problem I removed all java versions. 所以,因为这看起来像一个javac版本问题我删除了所有java版本。 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. 安装了1.7_51,安装了Apple提供的1.6版本,以便Eclipse运行,更改了sym链接,因此1.7是默认值。 Same problem as before, the same project would not compile, though others did, 和以前一样的问题,同样的项目不会编译,尽管其他人做了,

Next step, strip the problem down to the bare bones ie remove ant etc from the equation - Test.java file used has no methods, just a main with System.out.println(). 下一步,将问题删除到裸骨,即从等式中删除ant等 - 使用的Test.java文件没有方法,只有System.out.println()的main。 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: cd到包含源文件的目录并运行编译器指定javac的完整路径,检查未设置JAVA_HOME:

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

Output: 输出:

javac 1.7.0_51 javac 1.7.0_51

[parsing started Test.java] [解析开始Test.java]

[parsing completed 67ms] [解析完成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, .]] [类文件的搜索路径:[/ 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)] [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) Test.java:11:无法访问java.lang.Object坏类文件:/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 类文件有错误的版本51.0,应该是49.0

Please remove or make sure it appears in the correct subdirectory of the classpath. 请删除或确保它出现在类路径的正确子目录中。 public class Test { ^ 公共课测试{^

[total 336ms] 1 error [总共336ms] 1错误

Flumoxed. Flumoxed。

Any ideas ? 有任何想法吗 ?

On a related note I seem to be unable to get javac to accept a -target option above 1.5/5: 在相关的说明中,我似乎无法让javac接受1.5 / 5以上的-target选项:

$ /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/javac Test.java -verbose -extdirs . $ /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/bin/javac Test.java -verbose -extdirs。 -endorseddirs . -endorseddirs。 -cp . -cp。 -bootclasspath /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/rt.jar -version -target 1.7 -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 1.7.0_51

javac: invalid target release: 1.7 javac:无效的目标版本:1.7

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. 经过大量的讨论修复后发布 - 所有这些都没有成功 - 我使用dtrace来查看命令运行时javac正在访问哪些文件。

In terminal (you need XCode installed) 在终端(你需要安装XCode)

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 我在另一个终端实例中运行命令行,我发现它隐藏在javac的文件访问活动中 - 考虑到命令行选项,你会觉得你在限制它查找文件的位置 - 我发现

open:entry javac /Users/me/Library/Java/Extensions/tools.jar 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 所以,这是由旧罐子引起的版本不匹配 - 正如其他人发现的那样 - 我的案例中的问题是找到它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM