java.lang.ClassFormatError:常量池标签 18 无效
java.lang.ClassFormatError: Constant pool tag 18 is invalid
ID: java/class-format-error-constant-pool-tag
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Java 8 | active | — | — | — |
| Java 11 | active | — | — | — |
| Java 17 | active | — | — | — |
| Java 21 | active | — | — | — |
根因分析
当 JVM 尝试加载具有无效或不支持的常量池标签的类文件时发生此错误,通常由类文件版本与 JVM 版本不匹配或类文件损坏引起。
English
This error occurs when the JVM attempts to load a class file with an invalid or unsupported constant pool tag, typically caused by a mismatch between the class file version and the JVM version or corruption of the class file.
官方文档
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/ClassFormatError.html解决方案
-
Ensure all dependencies and the project are compiled with the same Java version. For Maven, set the maven-compiler-plugin source and target to match the runtime JVM.
-
If the error occurs from a third-party library, update the library to a version that matches your JVM, or exclude the problematic class and replace it with a newer version.
-
Check the class file version using javap -verbose and ensure it is compatible with the JVM. Regenerate the class file if needed.
无效尝试
常见但无效的做法:
-
90% 失败
Cleaning the project and rebuilding does not fix the underlying binary incompatibility or class corruption.
-
80% 失败
Recompiling with the same compiler version does not change the constant pool format; the issue is often from mixing class files from different Java versions.
-
95% 失败
The error is not related to JDK installation; reinstalling JDK does not fix class file corruption.