Java “不支持的major.minor 版本52.0”是什么意思,我该如何解决?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37612054/
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 19:28:49  来源:igfitidea点击:

What does 'Unsupported major.minor version 52.0' mean, and how do I fix it?

javaeclipseversion

提问by Andy

Ok so I loosely understand that 52.0 is Java 8, and that the exceptions means that some code is compiled with one version of java, and some with another. What I can't get my head around is which way around it is.

好的,所以我大致理解 52.0 是 Java 8,并且异常意味着某些代码是用一个版本的 java 编译的,而一些代码是用另一个版本编译的。我无法理解的是它是哪种方式。

Here's the stack trace that I get:

这是我得到的堆栈跟踪:

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/openrdf/model/ValueFactory : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access0(URLClassLoader.java:71)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
    at java.lang.Class.getMethod0(Class.java:2856)
    at java.lang.Class.getMethod(Class.java:1668)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)

I'm executing my code from eclipse and it crashes before reaching main. I've followed a few posts on here saying to change my compliance level/my JRE, but I can't seem to get it to fix.

我正在从 eclipse 执行我的代码,它在到达 main 之前崩溃了。我在这里关注了一些帖子,说要更改我的合规性级别/我的 JRE,但我似乎无法修复它。

Is the error saying that the class 'ValueFactory' has been compiled with java 8, or that my code has? I've tried changing my compliance level to 1.6, 1.7, and 1.8, but neither of these fixed the issue.

错误是说“ValueFactory”类是用java 8编译的,还是我的代码有?我尝试将我的合规性级别更改为 1.6、1.7 和 1.8,但这些都没有解决问题。

采纳答案by Mena

You don't needto change the compliance level here, or rather, you should but that's not the issue.

你并不需要改变这里的合规性水平,或者更确切地说,你应该,但是这不是问题。

The code compliance ensures your code is compatible with a given Java version.

代码合规性确保您的代码与给定的 Java 版本兼容。

For instance, if you have a code compliance targeting Java 6, you can't use Java 7's or 8's new syntax features (e.g. the diamond, the lambdas, etc. etc.).

例如,如果您有针对 Java 6 的代码合规性,则不能使用 Java 7 或 8 的新语法特性(例如菱形、lambdas 等)。

The actual issue here is that you are trying to compile something in a Java version that seems different from the project dependencies in the classpath.

这里的实际问题是您试图在 Java 版本中编译某些内容,而该版本似乎与类路径中的项目依赖项不同。

Instead, you should check the JDK/JRE you're using to build.

相反,您应该检查用于构建的 JDK/JRE。

In Eclipse, open the project properties and check the selected JRE in the Java build path.

在 Eclipse 中,打开项目属性并检查 Java 构建路径中的选定 JRE。

If you're using custom Ant (etc.) scripts, you also want to take a look there, in case the above is not sufficient per se.

如果您使用自定义 Ant(等)脚本,您还需要查看那里,以防上述内容本身不够用。

回答by R. Horber

Your code was compiled with Java 8.

您的代码是用 Java 8 编译的。

Either compile your code with an older JDK (compliance level) or run it on a Java 8 JRE.

使用较旧的 JDK(合规级别)编译您的代码或在 Java 8 JRE 上运行它。

Hope this helps...

希望这可以帮助...

回答by rsingh25

Actually you have a code compiled targeting a higher JDK (JDK 1.8 in your case) but at runtime you are supplying a lower JRE(JRE 7 or below).

实际上,您编译了一个针对更高 JDK(在您的情况下为 JDK 1.8)的代码,但在运行时您提供的是一个较低的 JRE(JRE 7 或更低版本)。

you can fix this problem by adding target parameter while compilation
e.g. if your runtime target is 1.7, you should use 1.7 or below

您可以通过在编译
时添加目标参数来解决此问题,例如,如果您的运行时目标是 1.7,则应使用 1.7 或更低版本

javac -target 1.7 *.java

if you are using eclipse, you can sent this parameter at Window -> Preferences -> Java -> Compiler -> set "Compiler compliance level" = choose your runtime jre version or lower.

如果您使用的是 eclipse,则可以在 Window -> Preferences -> Java -> Compiler -> set "Compiler compliance level" = 选择您的运行时 jre 版本或更低版本中发送此参数。