java runtime_error ai_generated true

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

其他格式: JSON · Markdown 中文 · English
92%修复率
90%置信度
1证据数
2023-03-10首次发现

版本兼容性

版本状态引入弃用备注
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.

generic

官方文档

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/UnsupportedClassVersionError.html

解决方案

  1. 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.
  2. Recompile the source code with a lower target version using javac -source 8 -target 8 (or --release 8) if the source is available.
  3. 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.

无效尝试

常见但无效的做法:

  1. 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.

  2. 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.

  3. Delete the JAR file and rebuild with same JDK 90% 失败

    If the build environment hasn't changed, the same version mismatch will persist.