java.lang.UnsupportedClassVersionError: org/example/MyClass 由更高版本的 Java 运行时编译(类文件版本 61.0),此 Java 运行时仅识别最高版本 52.0 的类文件
java.lang.UnsupportedClassVersionError: org/example/MyClass has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 52.0
ID: java/unsupported-major-minor-version
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Java 8 (class version 52.0) | active | — | — | — |
| Java 11 (class version 55.0) | active | — | — | — |
| Java 17 (class version 61.0) | active | — | — | — |
| Java 21 (class version 65.0) | active | — | — | — |
根因分析
编译的类文件的主版本(例如 Java 17 的 61)高于正在运行的 JVM 支持的版本(例如 Java 8 的 52),表明编译时和运行时的 Java 版本不匹配。
English
The compiled class file has a major version (e.g., 61 for Java 17) higher than what the running JVM supports (e.g., 52 for Java 8), indicating a mismatch between compile-time and runtime Java versions.
官方文档
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/UnsupportedClassVersionError.html解决方案
-
Upgrade the runtime JVM to match the compilation version. For example, if class version is 61.0, run with Java 17 or later. Set JAVA_HOME and update PATH accordingly.
-
Recompile the source code with a lower target version using javac -source 8 -target 8 (or --release 8) if the source is available.
-
Use a dependency management tool like Maven to enforce a consistent Java version across modules by setting <maven.compiler.source> and <maven.compiler.target> properties.
无效尝试
常见但无效的做法:
-
Set JAVA_HOME to an older JDK version and recompile
80% 失败
This downgrades the runtime but the compiled classes still have the higher version; the error occurs at runtime, not compile time.
-
Add --release flag to the compiler without changing the JDK
70% 失败
The --release flag only works if the correct JDK version is used; using it with an older JDK may still produce incompatible class files.
-
Delete the JAR file and rebuild with same JDK
90% 失败
If the build environment hasn't changed, the same version mismatch will persist.