You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决scalac编译报错:assertion failed(DefaultFormats类相关)

Scala编译错误:No RuntimeVisibleAnnotations in classfile with ScalaSignature attribute

问题描述

我是Scala编程新手,已将代码从Git拉取至IntelliJ中,但编译时出现如下scalac错误。我在Mac上安装了最新版本的Scala(3.2.0),同时也安装了JDK18,请问我遗漏了什么?

scalac: Error: assertion failed: 
  No RuntimeVisibleAnnotations in classfile with ScalaSignature attribute: class DefaultFormats
     while compiling: /Users/usr/mainProg.scala
        during phase: globalPhase=typer, enteringPhase=namer
     library version: version 2.13.9
    compiler version: version 2.13.9
  reconstructed args: -classpath <Several Jar file locations>
  last tree to typer: Ident(net)
       tree position: line 3 of /Users/usr/mainProg.scala
            tree tpe: net.type
              symbol: final package net
   symbol definition: final package net (a ModuleSymbol)
      symbol package: <none>
       symbol owners: package net
           call site: package processing in package delta in package delta

可能的原因及解决方法

  • Scala版本不匹配:错误日志显示编译器和库用的是2.13.9,但你安装的是Scala 3.2.0。Scala 3和Scala 2的字节码格式、编译器逻辑差异较大,混用会触发这类注解解析错误。

    • 操作:打开IntelliJ的File > Project Structure > Global Libraries,确认项目绑定的Scala SDK是3.2.0;同时检查build.sbt(或其他构建文件)里的scalaVersion设置,改成3.2.0。
  • 依赖库版本不兼容:DefaultFormats一般来自JSON序列化库(比如Play JSON、spray-json),如果这些依赖是基于Scala 2编译的,在Scala 3环境下会出现注解异常。

    • 操作:替换依赖为支持Scala 3的版本,比如Play JSON从2.9.0开始支持Scala 3;注意Scala 3的依赖后缀是_3,示例:"com.typesafe.play" %% "play-json" % "2.9.2"。
  • JDK版本适配问题:Scala 3.2.0对JDK 18的支持存在小概率兼容性问题,部分旧依赖在高版本JDK下会出现字节码解析错误。

    • 操作:降级JDK到11或17(LTS版本),这两个版本是Scala生态的主流兼容版本。在IntelliJ中通过File > Project Structure > Project Settings > Project切换SDK。
  • IntelliJ缓存干扰:IDE缓存可能导致版本配置不生效,引发编译异常。

    • 操作:执行File > Invalidate Caches... > Invalidate and Restart,清理缓存后重新编译项目。

内容的提问来源于stack exchange,提问作者djm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 14:05:29