# java.lang.UnsupportedClassVersionError: org/example/MyClass 由更高版本的 Java 运行时编译（类文件版本 61.0），此 Java 运行时仅识别最高版本 52.0 的类文件

- **ID:** `java/unsupported-major-minor-version`
- **领域:** java
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

编译的类文件的主版本（例如 Java 17 的 61）高于正在运行的 JVM 支持的版本（例如 Java 8 的 52），表明编译时和运行时的 Java 版本不匹配。

## 版本兼容性

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

## 解决方案

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

## 无效尝试

- **Set JAVA_HOME to an older JDK version and recompile** — This downgrades the runtime but the compiled classes still have the higher version; the error occurs at runtime, not compile time. (80% 失败率)
- **Add --release flag to the compiler without changing the JDK** — The --release flag only works if the correct JDK version is used; using it with an older JDK may still produce incompatible class files. (70% 失败率)
- **Delete the JAR file and rebuild with same JDK** — If the build environment hasn't changed, the same version mismatch will persist. (90% 失败率)
