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

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

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

javaversion

提问by grmn.bob

I am getting this error when I include an opensource library that I had to compile from source. Now, all the suggestions on the web indicate that the code was compiled in one version and executed in another version (new on old). However, I only have one version of JRE on my system. If I run the commands:

当我包含必须从源代码编译的开源库时,出现此错误。现在,网络上的所有建议都表明代码是在一个版本中编译并在另一个版本中执行的(新旧)。但是,我的系统上只有一个版本的 JRE。如果我运行命令:

$ javac -version
javac 1.5.0_18

$ java -version
java version "1.5.0_18"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_18-b02)
Java HotSpot(TM) Server VM (build 1.5.0_18-b02, mixed mode)

and check in Eclipse for the properties of the java library, I get 1.5.0_18

并在 Eclipse 中检查 java 库的属性,我得到 1.5.0_18

Therefore, I have to conclude something else, internal to a class itself, is throwing the exception?? Is that even possible?

因此,我必须得出结论,类本身内部的其他东西正在抛出异常??这甚至可能吗?

采纳答案by Joshua McKinnon

I've learned that error messages like this are usually right. When it couldn't POSSIBLY (in your mind) be what the error being reported says, you go hunting for a problem in another area...only to find out hours later that the original error message was indeed right.

我了解到像这样的错误消息通常是正确的。当它不可能(在您看来)是所报告的错误所说的内容时,您会去寻找另一个区域的问题……但几个小时后才发现原始错误消息确实是正确的。

Since you're using Eclipse, I think Thilo has it right The most likely reason you are getting this message is because one of your projects is compiling 1.6 classes. It doesn't matter if you only have a 1.5 JRE on the system, because Eclipse has its own compiler (not javac), and only needs a 1.5 JRE to compile 1.6 classes. It may be weird, and a setting needs to be unchecked to allow this, but I just managed to do it.

由于您使用的是 Eclipse,我认为 Thilo 是正确的。您收到此消息的最可能原因是您的一个项目正在编译 1.6 类。如果系统上只有 1.5 JRE 没有关系,因为 Eclipse 有自己的编译器(不是 javac),并且只需要 1.5 JRE 来编译 1.6 类。这可能很奇怪,需要取消选中设置才能允许这样做,但我只是设法做到了。

For the project in question, check the Project Properties (usually Alt+Enter), Java Compiler section. Here's an image of a project configured to compile 1.6, but with only a 1.5 JRE.

对于有问题的项目,检查项目属性(通常是 Alt+Enter),Java 编译器部分。这是配置为编译 1.6 但只有 1.5 JRE 的项目的图像。

enter image description here

在此处输入图片说明

回答by Thilo

Did you compile with Eclipse? It uses a different compiler (not javac). That should not result in this error (if everything is configured properly), but you can try to compile it with javac instead.

你是用 Eclipse 编译的吗?它使用不同的编译器(不是 javac)。这不应该导致此错误(如果一切配置正确),但您可以尝试使用 javac 编译它。

If that fixed the problem, try to see if Eclipse has some incorrect compiler settings. Specifically have it target Java 5.

如果这解决了问题,请尝试查看 Eclipse 是否有一些不正确的编译器设置。特别是让它针对 Java 5。

回答by Brent Writes Code

Have you tried doing a full "clean" and then rebuild in Eclipse (Project->Clean...)?

您是否尝试过完全“清理”然后在 Eclipse 中重建(项目->清理...)?

Are you able to compile and run with "javac" and "java" straight from the command line? Does that work properly?

您是否能够直接从命令行编译和运行“javac”和“java”?这能正常工作吗?

If you right click on your project, go to "Properties" and then go to "Java Build Path", are there any suspicious entries under any of the tabs? This is essentially your CLASSPATH.

如果您右键单击您的项目,转到“属性”,然后转到“Java 构建路径”,在任何选项卡下是否有任何可疑条目?这本质上是您的 CLASSPATH。

In the Eclipse preferences, you may also want to double check the "Installed JREs" section in the "Java" section and make sure it matches what you think it should.

在 Eclipse 首选项中,您可能还需要仔细检查“Java”部分中的“已安装 JRE”部分,并确保它与您认为应该匹配的内容相匹配。

You definitely have either a stale .class file laying around somewhere or you're getting a compile-time/run-time mismatch in the versions of Java you're using.

您肯定在某处放置了一个陈旧的 .class 文件,或者您正在使用的 Java 版本中出现编译时/运行时不匹配。

回答by Jay

Another scenario where this could happen is when you are launching an instance of eclipse (for debug etc.) from a host eclipse - in which case, altering the project's level or JRE library on the project's classpath alone doesn't help. What matters is the JRE used to launch the target eclipse environment.

另一种可能发生这种情况的情况是,当您从主机 eclipse 启动 eclipse 实例(用于调试等)时 - 在这种情况下,仅更改项目类路径上的项目级别或 JRE 库无济于事。重要的是用于启动目标 eclipse 环境的 JRE。

回答by Jeshurun

Also check any jar files in your project that have been compiled for a higher version of Java. If these are your own libraries, you can fix this by changing the target version attribute to javac

还要检查项目中为更高版本的 Java 编译的所有 jar 文件。如果这些是您自己的库,您可以通过将目标版本属性更改为 javac 来解决此问题

<javac destdir="${classes.dir}"
            debug="on" classpathref="project.classpath" target="1.6">

回答by James Drinkard

Always check for the obvious too. I got this error once when I accidently grabbed the wrong resource for the server's add and remove action. It can be easy to overlook.

也总是检查明显的。当我不小心为服务器的添加和删除操作抓取了错误的资源时,我收到了一次此错误。这很容易被忽视。

回答by user3471219

Deleting the project specific settings files (Eclipse workspace/project folder/.settings/) from the project folder also will do. Obviously, we need to do a project clean and build after deleting.

从项目文件夹中删除项目特定的设置文件(Eclipse 工作区/项目文件夹/.settings/)也可以。很明显,我们需要在删除后做一个项目clean和build。

回答by Vikky

I also got the same error. Reason for that I was compiling the project using Maven. I had JAVA_HOME pointing to JDK7 and hence java 1.7 was being used for compilation and when running the project I was using JDK1.5. Changing the below entry in .classpath file or change in the eclipse as in the screenshot resolved the issue.

我也遇到了同样的错误。原因是我正在使用 Maven 编译项目。我让 JAVA_HOME 指向 JDK7,因此 Java 1.7 被用于编译,并且在运行项目时我使用的是 JDK1.5。更改 .classpath 文件中的以下条目或更改 eclipse 中的屏幕截图解决了该问题。

classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5

or change in the run configuarions of eclipse as enter image description here

或将 eclipse 的运行配置更改为 在此处输入图片说明

回答by Li3ro

changing the project to use java 1.7: For this to work follow those steps :

将项目更改为使用 java 1.7:为此,请按照以下步骤操作:

  • Change Compiler Compliance Level
  • Change your projects JRE/JDK to something of the same level(1.7 in my case)
  • Do the same change in all the projects referenced by your project
  • Change your run/debug configuration to use the JRE/JDK (or of the same level)
  • 更改编译器合规性级别
  • 将您的项目 JRE/JDK 更改为相同级别(在我的情况下为 1.7)
  • 在您的项目引用的所有项目中进行相同的更改
  • 更改您的运行/调试配置以使用 JRE/JDK(或相同级别)

Not working?

不工作?

  • delete projects Bin directory
  • Clean
  • reBuild
  • 删除项目Bin目录
  • 干净的
  • 重建

Still not working?

还是行不通?

in your project's directory: edit .settings/org.eclipse.jdt.core.prefs > make sure your target level is applied

在您的项目目录中:编辑 .settings/org.eclipse.jdt.core.prefs > 确保应用了您的目标级别

Good Luck!

祝你好运!

回答by ocrampico

i solved this problem by changing jre required for server (in my case is tomcat). From Server tab in eclipse, double click on the server (in order to open page for server configuration), click on Runtime environment, then change JRE required

我通过更改服务器所需的 jre 解决了这个问题(在我的例子中是 tomcat)。在 eclipse 的 Server 选项卡中,双击服务器(为了打开服务器配置页面),单击 Runtime environment,然后更改所需的 JRE