1. Introduction
1.绪论
In this short tutorial, we’re going to learn what causes the Java runtime error java.lang.UnsupportedClassVersionError: Unsupported major.minor version and how to fix it.
在这个简短的教程中,我们将学习导致Java运行时错误的原因java.lang.UnsupportedClassVersionError:不支持的major.minor版本以及如何修复它。
2. A Look at the Error
2.看一看错误
Let’s start by looking at an example error:
让我们先看看一个错误的例子。
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/baeldung/MajorMinorApp
has been compiled by a more recent version of the Java Runtime (class file version 55.0),
this version of the Java Runtime only recognizes class file versions up to 52.0
This error is telling us that our class was compiled at a higher version of Java than the version with which we tried to run it. More specifically, in this case we compiled our class with Java 11 and tried to run it with Java 8.
这个错误告诉我们,我们的类是在一个比我们试图运行它的版本更高的Java版本上编译的。更具体地说,在这种情况下,我们用Java 11编译了我们的类,并试图用Java 8运行它。
2.1. Java Version Numbers
2.1 Java版本号
For reference, let’s take a quick look at the Java version numbers. This will come in handy in case we need to download the appropriate Java version.
作为参考,让我们快速浏览一下Java的版本号。这在我们需要下载适当的Java版本时将会很方便。
The major and minor version numbers are stored in the class bytecode at bytes six and seven.
主要和次要版本号存储在类的字节6和7中。
Let’s see how the major version numbers map to Java versions:
让我们看看主要版本号是如何与Java版本对应的。
- 45 = Java 1.1
- 46 = Java 1.2
- 47 = Java 1.3
- 48 = Java 1.4
- 49 = Java 5
- 50 = Java 6
- 51 = Java 7
- 52 = Java 8
- 53 = Java 9
- 54 = Java 10
- 55 = Java 11
- 56 = Java 12
- 57 = Java 13
3. Fix via the Command Line
3.通过命令行修复
Now let’s discuss how we can resolve this error when running Java from the command line.
现在我们来讨论一下,当从命令行运行Java时,我们如何解决这个错误。
Depending on our situation, we have two ways we can resolve this error: compile our code for an earlier version of Java, or run our code on a newer Java version.
根据我们的情况,我们有两种方法可以解决这个错误:为早期的Java版本编译我们的代码,或者在较新的Java版本上运行我们的代码。
The final decision depends on our situation. If we need to use a third-party library that’s already been compiled at a higher level, our best option is probably to run our application using a newer Java version. If we’re packaging an application for distribution, it might be best to compile down to an older version.
最后的决定取决于我们的情况。如果我们需要使用一个已经被编译过的第三方库,我们最好的选择可能是使用较新的Java版本运行我们的应用程序。如果我们要把一个应用程序打包发布,最好是编译成一个较旧的版本。
3.1. JAVA_HOME Environment Variable
3.1.JAVA_HOME环境变量
Let’s start by checking how our JAVA_HOME variable is set. This will tell us which JDK is being used when we run javac from our command line:
让我们开始检查我们的JAVA_HOME变量是如何设置的。这将告诉我们,当我们从命令行运行javac时,正在使用哪个JDK。
echo %JAVA_HOME%
C:\Apps\Java\jdk8-x64
If we’re ready to move entirely to a newer JDK, we can download the newer version and make sure our PATH and JAVA_HOME environment variables are set appropriately.
如果我们准备完全转移到较新的JDK,我们可以下载较新的版本,并确保我们的PATH和JAVA_HOME环境变量被适当设置。
3.2. Running a New JRE
3.2.运行一个新的JRE
Returning to our example, let’s look at how we can resolve the error by running it at a higher version of Java. Assuming we have Java 11 JRE in C:\Apps\jdk-11.0.2, we can run our code with the java command packaged with it:
回到我们的例子,让我们看看如何通过在更高版本的Java中运行来解决这个错误。假设我们在C:\Apps\jdk-11.0.2中拥有Java 11 JRE,我们可以用与之打包的java命令运行我们的代码。
C:\Apps\jdk-11.0.2\bin\java com.baeldung.MajorMinorApp
Hello World!
3.3. Compiling With an Older JDK
3.3.用较早的JDK进行编译
If we’re writing an application that we want to be runnable down to a certain version of Java, we need to compile the code for that version.
如果我们编写的应用程序希望可以运行到某一版本的Java,我们需要为该版本的代码进行编译。
We can do that in one of three ways: using an older JDK to compile our code; using the -bootclasspath, -source, and -target options of the javac command (JDK 8 and older); or using the –release option (JDK 9 and newer).
我们可以通过以下三种方式之一来实现:使用较早的JDK来编译我们的代码;使用-bootclasspath、-source和-target命令的选项(JDK 8及更早版本);或者使用-release选项(JDK 9及更新版本)。
Let’s start by using an older JDK, similar to how we used a newer JRE for running our code:
让我们先使用一个较早的JDK,类似于我们使用较新的JRE来运行我们的代码。
C:\Apps\Java\jdk1.8.0_31\bin\javac com/baeldung/MajorMinorApp.java
It’s possible to just use -source and -target, but it might still create class files that aren’t compatible with an older Java.
可以只使用-source和-target,但它仍然可能创建与旧版Java不兼容的类文件。
To ensure compatibility, we can point -bootclasspath at the rt.jar of the targeted JRE:
为了确保兼容性,我们可以将-bootclasspath指向目标JRE的rt.jar。
javac -bootclasspath "C:\Apps\Java\jdk1.8.0_31\jre\lib\rt.jar" \
-source 1.8 -target 1.8 com/baeldung/MajorMinorApp.java
The above applies mainly to JDK 8 and lower. In JDK 9, the –release parameter was added to replace -source and -target. The –release option supports targets 6, 7, 8, 9, 10, and 11.
上述内容主要适用于JDK 8及以下版本。在JDK 9中,添加了-release参数来取代-source和-target。-release 选项支持目标 6、7、8、9、10 和 11。
Let’s use –release to target Java 8:
让我们使用-release来针对Java 8。
javac --release 8 com/baeldung/MajorMinorApp.java
Now we can run our code on a Java 8 or higher JRE.
现在我们可以在Java 8或更高的JRE上运行我们的代码。
4. Eclipse IDE
4.Eclipse IDE
Now that we understand the error and the general approach to correcting it, let’s take what we’ve learned and see how we can apply it when working in the Eclipse IDE.
现在我们了解了这个错误和纠正它的一般方法,让我们把学到的东西拿出来,看看在Eclipse IDE中工作时如何应用它。
4.1. Changing the JRE
4.1.改变JRE
Assuming we already have Eclipse configured with different versions of Java, let’s change our project’s JRE.
假设我们已经将Eclipse配置为不同版本的Java,让我们改变我们项目的JRE。
Let’s go to our Project properties, then Java Build Path, and then the Libraries tab. Once there, we’ll select the JRE and click Edit:
让我们进入我们的项目属性,然后Java构建路径,然后库标签。在那里,我们将选择JRE并点击Edit。
Now let’s choose Alternate JRE and point to our Java 11 installation:
现在让我们选择Alternate JRE并指向我们的Java 11安装。
At this point, our application will run against Java 11.
在这一点上,我们的应用程序将针对Java 11运行。
4.2. Changing the Compiler Level
4.2.改变编译器级别
Now let’s look at how we can change our target to a lower level of Java.
现在让我们来看看如何将我们的目标改为较低层次的Java。
First, let’s go back to our Project properties, then Java Compiler, and then check Enable project specific settings:
首先,让我们回到项目属性,然后Java编译器,然后选中启用项目特定设置。
Here we can set our project to compile for earlier versions of Java and customize other compliance settings:
在这里,我们可以设置我们的项目为早期的Java版本进行编译,并定制其他的合规设置。
5. IntelliJ IDEA
5 IntelliJ IDEA
We can also control the version of Java we’re using for compiling and running in IntelliJ IDEA.
我们还可以控制在IntelliJ IDEA中编译和运行所使用的Java的版本。
5.1. Adding a JDK
5.1.添加一个JDK
Before we do that, we’ll see how to add additional JDKs. Let’s go to File -> Project Structure -> Platform Settings -> SDKs:
在这之前,我们先看看如何添加额外的JDKs。让我们进入文件 -> 项目结构 -> 平台设置 -> SDKs。
Let’s click the plus icon in the middle column, select the JDK from the drop-down, and select our JDK location:
让我们点击中间一栏的加号图标,从下拉菜单中选择JDK,并选择我们的JDK位置。
5.2. Changing the JRE
5.2.改变JRE
First, we’ll look at how to use IDEA to run our project on the newer JRE.
首先,我们来看看如何使用IDEA在较新的JRE上运行我们的项目。
Let’s go to Run -> Edit Configurations… and change our JRE to 11:
让我们进入Run -> Edit Configurations…并将我们的JRE改为11。
Now when we run our project, it will run with the Java 11 JRE.
现在,当我们运行我们的项目时,它将用Java 11 JRE运行。
5.3. Changing the Compiler Level
5.3.改变编译器级别
If we’re distributing our application to run on a lower JRE, we need to adjust our compiler level to target the older version of Java.
如果我们要把我们的应用程序发布到一个较低的JRE上运行,我们需要调整我们的编译器级别,以针对较旧的Java版本。
Let’s go to File -> Project Structure… -> Project Settings -> Project and change our Project SDK and Project language level:
让我们进入文件 -> 项目结构… -> 项目设置 -> 项目,改变我们的项目SDK和项目语言级别。
We can now build our project, and the class files generated will run on Java 8 and higher.
现在我们可以构建我们的项目,生成的类文件将在Java 8和更高版本上运行。
6. Maven
6.雯雯
When we build and package a file in Maven, we can control the version of Java we target.
当我们在Maven中构建和打包一个文件时,我们可以控制我们所针对的Java版本。
When using Java 8 or older, we set the source and target for the compiler plugin.
当使用Java 8或更高版本时,我们为编译器插件设置源和目标。
Let’s set the source and target using compiler plugin properties:
让我们使用编译器插件属性来设置源和目标。
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
Alternatively, we can set the source and target in the compiler plugin:
另外,我们可以在编译器插件中设置源和目标。
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
With the –release option added in Java 9, we can configure that with Maven as well.
由于Java 9中增加了-release选项,我们也可以用Maven进行配置。
Let’s use a compiler plugin property to set the release:
让我们使用一个编译器插件属性来设置release。
<properties>
<maven.compiler.release>8</maven.compiler.release>
</properties>
Or we can configure the compiler plugin directly:
或者我们可以直接配置编译器插件。
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<release>8</release>
</configuration>
</plugin>
</plugins>
7. Conclusion
7.结语
In this article, we learned what causes the java.lang.UnsupportedClassVersionError: Unsupported major.minor version error message, and how to fix it.
在这篇文章中,我们了解了导致java.lang.UnsupportedClassVersionError:Unsupported major.minor version错误信息,以及如何修复它。