java.lang.UnsupportedClassVersionError: .class 文件中的错误版本号

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

java.lang.UnsupportedClassVersionError: Bad version number in .class file

javamacosunsupported-class-version

提问by dotty

I'm having this error in eclipes java.lang.UnsupportedClassVersionError: Bad version number in .class file.

我在 eclipes 中遇到了这个错误java.lang.UnsupportedClassVersionError: Bad version number in .class file

When I run javac -versionit prints

当我运行javac -version它打印

javac 1.5.0_28

Within eclipes if I right click, properties->java compiler it says I'm using 1.5and if I then go to java build path, I have JRE System Library [JVM 1.5.0 (MacOS X Default)]in there.

在日食中,如果我右键单击,属性-> java 编译器它说我正在使用1.5,然后如果我转到java build path,我就JRE System Library [JVM 1.5.0 (MacOS X Default)]在那里。

Why am i getting this error and how do I fix it?

为什么我会收到这个错误,我该如何解决?

EDITS

编辑

java -versionprints

java -version印刷

java version "1.5.0_28"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_28-b04-382-9M3326)
Java HotSpot(TM) Client VM (build 1.5.0_28-157, mixed mode, sharing)

The other libraries in my application is the libgdx library.

我的应用程序中的其他库是 libgdx 库。

The full error is

完整的错误是

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:676)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access0(URLClassLoader.java:56)
at java.net.URLClassLoader.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:317)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:375)

采纳答案by Jesper

You get this error when you try to run a class that was compiled for a Java version newer than what you have; for example, if you try to use a class that was compiled for Java 6 or newer on a Java 5 JVM.

当您尝试运行为比您拥有的 Java 版本更新的 Java 版本编译的类时,您会收到此错误;例如,如果您尝试在 Java 5 JVM 上使用为 Java 6 或更新版本编译的类。

It doesn't necessarily have to be your own class; you might be using some library that was built for Java 6 or newer.

它不一定是你自己的类;您可能正在使用一些为 Java 6 或更新版本构建的库。

Are you using specific libraries (JAR files)? Check if these are compatible with Java 5 or not. Or upgrade your Java version to Java 6.

您是否使用特定的库(JAR 文件)?检查这些是否与 Java 5 兼容。或者将您的 Java 版本升级到 Java 6。

回答by gabuzo

My guess is that you are using 3rd party libraries built with Java 1.6.

我的猜测是您使用的是用 Java 1.6 构建的 3rd 方库。

By the way is there any reason you are using Java 1.5? Java 1.6 is out for long and Java 1.5 will no longer be supported real soon (if not already).

顺便说一句,您使用 Java 1.5 有什么理由吗?Java 1.6 已经发布很久了,而且 Java 1.5 很快将不再受支持(如果还没有的话)。

回答by Thorbj?rn Ravn Andersen

You have a class compiled for a newer Java than the one you are using.

您为比您正在使用的 Java 更新的 Java 编译了一个类。

The stack trace should show the name of the troublesome class. Perhaps it is a library you are using?

堆栈跟踪应显示有问题的类的名称。也许这是您正在使用的图书馆?

回答by JohnGH

You may find that the version of java that is being used is not the copy that the code was expecting.

您可能会发现正在使用的 java 版本不是代码期望的副本。

For example, if your $PATH (this example is on Unix) is set to /usr/bin:(other directories) you would get /usr/bin/java when you run java:

例如,如果您的 $PATH(此示例在 Unix 上)设置为 /usr/bin:(其他目录),您将在运行 java 时得到 /usr/bin/java:

    $ java -version
    java version "1.5.0_18"
    Java(TM) Platform, Standard Edition for Business (build 1.5.0_18-b02)
    Java HotSpot(TM) Server VM (build 1.5.0_18-b02, mixed mode)
    $ which java
    /usr/bin/java

...but your program needs something like this to work:

...但你的程序需要这样的东西才能工作:

    $ /usr/local/java/jdk1.6/bin/java -version
    java version "1.6.0_14"
    Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
    Java HotSpot(TM) Server VM (build 14.0-b16, mixed mode)

so the fix would be to add the directory where your correct version of java lives to the start of your path before calling java, or call the good version java by its absolute path.

所以修复方法是在调用java之前将正确版本的java所在的目录添加到路径的开头,或者通过绝对路径调用好的版本java。

回答by Mustafa Z

If upgrading your JRE is not an option, you can compile your code with lower java version.

如果升级您的 JRE 不是一个选项,您可以使用较低的 Java 版本编译您的代码。

From eclipise:

来自日食:

Project properties > Java compiler > Compiler compliance level> [Set to whatever JRE version you have]

项目属性 > Java 编译器 > 编译器合规性级别 > [设置为您拥有的任何 JRE 版本]

See below screen for setting JRE 1.5 compatibility

请参阅下面的屏幕以设置 JRE 1.5 兼容性