Java 如何修复此错误:类文件的版本错误为 52.0,应为 49.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30078829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:04:48  来源:igfitidea点击:

how to fix this error: class file has wrong version 52.0, should be 49.0

javamacos

提问by Richard G

I'm really struggling with this. I've been unable to configure Intellij to use Javac, unable to run javadoc. I just can't seem to get my java runtime environment stable and working productively on Mac OSX.

我真的很挣扎。我一直无法将 Intellij 配置为使用 Javac,无法运行 javadoc。我似乎无法让我的 Java 运行时环境稳定并在 Mac OSX 上高效工作。

I've created the most basic of examples which is a hello world program and it still errors from command line. Everything I read about this error tells me to "upgrade the compiler". But I'm really not sure what the problem is, the javac & java are both running 1.8 (now, after I tried upgrading the system JVM).

我已经创建了最基本的示例,它是一个 hello world 程序,但它仍然从命令行出错。我读到的有关此错误的所有信息都告诉我“升级编译器”。但我真的不确定问题是什么,javac 和 java 都运行 1.8(现在,在我尝试升级系统 JVM 之后)。

I explicitly set the path when calling javac, checked the version etc. Everything tells me it's 1.8.

我在调用 javac 时明确设置了路径,检查了版本等。一切都告诉我它是 1.8。

Can anyone explain how to fix this? (without reverting my system to a 1.5 SDK).

谁能解释一下如何解决这个问题?(无需将我的系统恢复为 1.5 SDK)。

This is output from the basic hello world application:

这是基本的 hello world 应用程序的输出:

$JAVA_HOME/bin/javac Main.java
Main.java:3: cannot access java.lang.Object
bad class file: /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/jre/lib/rt.jar(java/lang/Object.class)
class file has wrong version 52.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
public class Main {
       ^
1 error
localhost:mycompany$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
localhost:mycompany$

Output from javac version:

javac 版本的输出:

javac -version
javac 1.8.0_45
javac: no source files
Usage: javac <options> <source files>

Output from java version:

java版本的输出:

java -version
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

采纳答案by Aaron Digulla

The error message means that you try to load a Java 8 class with a Java 5 VM.

该错误消息意味着您尝试使用 Java 5 VM 加载 Java 8 类。

For an unknown reason $JAVA_HOME/bin/javacis using the JDK 8 Java runtime (.../rt.jar) but the compiler itself is loaded with a Java 5 VM.

由于未知原因,$JAVA_HOME/bin/javac正在使用 JDK 8 Java 运行时 ( .../rt.jar),但编译器本身已加载 Java 5 VM。

My next step would be to make sure that $JAVA_HOME/bin/javacis actually what it should be (and not a script which does something weird). I'd also check the environment (CLASSPATH) and options like -Xbootclasspath. Try set | grep -i javaand set | grep -i pathto look for suspicious variables.

我的下一步将是确保这$JAVA_HOME/bin/javac实际上是它应该的样子(而不是一个会做一些奇怪事情的脚本)。我还会检查环境 ( CLASSPATH) 和诸如-Xbootclasspath. 尝试set | grep -i javaset | grep -i path寻找可疑的变量。

If nothing seems to work, try to reinstall the Java 8 SDK again.

如果似乎没有任何效果,请再次尝试重新安装 Java 8 SDK。

回答by B. Burn

I had exactly the same issue with the same versions when trying to build the Arduino IDE using ant. The fix is to remove an old version of tools.jarfrom /Library/Java/Extensions/- just wanted to clarify for others that hit the same issue.

在尝试使用 ant 构建 Arduino IDE 时,我遇到了相同版本的完全相同的问题。解决方法是删除旧版本的tools.jar/Library/Java/Extensions/-只是想澄清别人击中了同样的问题。