eclipse eclipse中的java.lang.UnsupportedClassVersionError

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

java.lang.UnsupportedClassVersionError in eclipse

javaeclipse

提问by Derek

I am not a Java programmer really, so I am posting this question. The exception is being thrown java.lang.UnsupportedClassVersionError in my main class in an eclipse project. If I comment out the imports that this class has, it compiles and runs fine. If I put the imports back in, it does not work. Does this mean that the libraries I am importing were compiled with a newer or older version of java than I have? when i do java -version on the system i get 1.5_07

我真的不是 Java 程序员,所以我发布了这个问题。在 eclipse 项目的主类中抛出 java.lang.UnsupportedClassVersionError 异常。如果我注释掉这个类的导入,它会编译并运行良好。如果我把进口放回去,它就不起作用。这是否意味着我正在导入的库是用比我新的或旧版本的 java 编译的?当我在系统上执行 java -version 时,我得到 1.5_07

I could've sworn this was actually working last week, but maybe some setting in eclipse got tweaked? Is the Java Build Path in eclipse what I need to look for to check the JRE and compiler versions?

我可以发誓这实际上在上周有效,但也许 eclipse 中的某些设置被调整了?Eclipse 中的 Java 构建路径是我需要查找的以检查 JRE 和编译器版本吗?

回答by Christian Semrau

In Eclipse, the compiler version is set independent of the JRE version. That means you can set the compiler to Java version 1.6 and use the JRE 1.5. In this case, you compiled classes cannot be run.

在 Eclipse 中,编译器版本的设置独立于 JRE 版本。这意味着您可以将编译器设置为 Java 1.6 版并使用 JRE 1.5。在这种情况下,您编译的类无法运行。

You can check the preferences in Window / Preferences / Java / Compilerfor the default compiler compliance level, or the properties of your project for a project specific compiler compliance level. Compare that level with the JRE used in your project (Project / Properties / Java Build Path -> Libraries / JRE System Library) and in your program's launch configuration (via the Run / Run Configurations...menu).

您可以检查Window / Preferences / Java / Compiler默认编译器合规级别的首选项,或项目特定编译器合规级别的项目属性。将该级别与项目 ( Project / Properties / Java Build Path -> Libraries / JRE System Library) 和程序启动配置(通过Run / Run Configurations...菜单)中使用的 JRE 进行比较。

回答by Jesper

UnsupportedClassVersionErrormeans that the Java runtime environment you are using doesn't recognise the version of a class file that you are trying to execute. The most common cause for this error is trying to use a class file compiled for a newer Java version on an older Java version - for example, you are trying to use a class compiled for Java 6 on a Java 5 runtime environment.

UnsupportedClassVersionError意味着您使用的 Java 运行时环境无法识别您尝试执行的类文件的版本。此错误的最常见原因是尝试在较旧的 Java 版本上使用为较新的 Java 版本编译的类文件 - 例如,您尝试在 Java 5 运行时环境中使用为 Java 6 编译的类。

As Eugene explained, Eclipse has its own built-in compiler, it does not use the compiler from the JDK - so that's how you can end up with Java 6 class files even if you're running on Java 5.

正如 Eugene 解释的那样,Eclipse 有自己的内置编译器,它不使用来自 JDK 的编译器——所以即使你在 Java 5 上运行,你也可以最终得到 Java 6 类文件。

Christian explains how to set the Java class file version in Eclipse.

Christian 解释了如何在 Eclipse 中设置 Java 类文件版本。

回答by iowatiger08

If you are using Maven with Eclipse, you can use the Maven plugin to update the project and then do a Maven "clean" . This resolved a goofy and unexpected show of this error in my case.

如果您将 Maven 与 Eclipse 一起使用,您可以使用 Maven 插件来更新项目,然后执行 Maven "clean" 。在我的情况下,这解决了此错误的愚蠢和意外显示。

回答by thonnor

I know this is an OLD thread now but I had a recent adventure with this type of error.

我现在知道这是一个旧线程,但我最近遇到了这种类型的错误。

When trying to compile my project within Eclipse using an Ant build file.

尝试使用 Ant 构建文件在 Eclipse 中编译我的项目时。

  • Eclipse Kepler (Java 1.7)
  • Ant 1.7
  • Project = Java 1.5
  • Eclipse 开普勒 (Java 1.7)
  • 蚂蚁 1.7
  • 项目 = Java 1.5

All internal paths and compiler settings were amended to look at Java 1.5 and the project built fine in Eclipse.

修改所有内部路径和编译器设置以查看 Java 1.5 和在 Eclipse 中构建良好的项目。

When trying to compile using Ant I got the java.lang.UnsupportedClassVersionErrorin eclipse and began looking around - including on here and finding this page.

当尝试使用 Ant 进行编译时,我得到了java.lang.UnsupportedClassVersionErrorEclipse 并开始环顾四周 - 包括在此处并找到此页面。

My resolution was this :

我的决议是这样的:

Windows > Preferences > Ant > Runtime > Global Entries

Global Entries was pointing at the Java 1.7 tools.jar

Global Entries 指向的是 Java 1.7 tools.jar

I added a new External Jar (on the right menu) and pointed it at the Java 1.5 tools.jar

我添加了一个新的 External Jar(在右侧菜单上)并将其指向 Java 1.5 tools.jar

I removed the original entry (for 1.7 tools.jar) and my ant builds started working.

我删除了原始条目(用于 1.7 tools.jar)并且我的 ant 构建开始工作。

回答by Eugene Kuleshov

Eclipse is not using JDK compiler, but has its own compiler which can produce bytecode for any JRE. You need to make sure that compiler settings in your project are set to the same or lower version as the JRE you have registered in Eclipse. See Window / Preferences / Java / Installed JREs and also check what JRE is used in your launch configuration (see Run menu for that).

Eclipse 没有使用 JDK 编译器,但有自己的编译器,可以为任何 JRE 生成字节码。您需要确保项目中的编译器设置设置为与您在 Eclipse 中注册的 JRE 相同或更低的版本。请参阅窗口/首选项/Java/已安装的 JRE,并检查您的启动配置中使用的 JRE(请参阅运行菜单)。

回答by Christian Semrau

If your imported classes come from an external library (i.e., not compiled from sources within your project), then you should validate that the JRE used for your project is new enough for this library.

如果您导入的类来自外部库(即,不是从您的项目中的源代码编译),那么您应该验证用于您的项目的 JRE 是否足够新的库。