Scala项目升级适配Java 8时遭遇类文件损坏错误,求解决方案
I’ve dealt with this exact headache when moving Scala projects to Java 8, so let’s get straight to solving it.
First, let’s recap the error you’re seeing (I’ve run into this exact stack trace multiple times):
[error] error while loading CharSequence, class file '/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.101-3.b13.el6_8.x86_64/jre/lib/rt.jar(java/lang/CharSequence.class)' is broken [error] (bad constant pool tag 18 at byte 10) [error] error while loading AnnotatedElement, class file '/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.101-3.b13.el6_8.x86_64/jre/lib/rt.jar(java/lang/reflect/AnnotatedElement.class)' is broken
What’s causing this?
This happens because older Scala versions don’t properly support the changes to Java’s class file format introduced in Java 8 (even Java 7 added the INVOKEDYNAMIC instruction—constant pool tag 18 refers to this feature). The outdated Scala compiler can’t parse the newer class files from the Java 8 runtime, hence the "broken class file" errors.
The Fix: Upgrade Your Scala Version
The only reliable way to resolve this is to move to a Scala version that’s fully compatible with Java 8:
- Scala 2.11.x: Every release in this line supports Java 8. Stick to the latest patch release (like 2.11.12) for the most bug fixes and stability.
- Scala 2.12.x+: These versions are built from the ground up for Java 8, so they’ll handle the Java 8 class file format seamlessly. They also natively support Java 8 features like lambdas, which is a nice bonus.
Update Your Build Configuration
Once you pick your target Scala version, update your build tool settings. For example, if you’re using sbt, edit your build.sbt file:
scalaVersion := "2.11.12" // Replace with your chosen compatible version
After updating, run a clean build (sbt clean compile) to clear out old artifacts and ensure the new Scala compiler processes everything correctly.
内容的提问来源于stack exchange,提问作者Shreek

