java.lang.NoSuchMethodError: 'void akka.actor.ExtensionId.$init$(akka.actor.ExtensionId)'

java.lang.NoSuchMethodError: 'void akka.actor.ExtensionId.$init$(akka.actor.ExtensionId)'

我只是想使用 akka 在 scala 项目中实现一个小示例 REST 端点。 代码如下所示

import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}

import akka.actor.typed.ActorSystem
import akka.actor.typed.scaladsl.Behaviors
import scala.io.StdIn

import scala.concurrent.ExecutionContextExecutor


object ViewAPI :
  @main def run(): Unit = {
    implicit val system = ActorSystem(Behaviors.empty, "my-system")
    implicit val executionContext = system.executionContext
    val route =
      path("hello") {
        get {
          complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
        }
      }
    val bindingFuture = Http().newServerAt("localhost", 9001).bind(route)

  }

我的 sbt 文件如下所示:

libraryDependencies += ("com.typesafe.akka" %% "akka-http" % "10.2.9").cross(CrossVersion.for3Use2_13)

libraryDependencies += "com.typesafe.akka" %% "akka-actor-typed" % "2.6.19"

libraryDependencies += "com.typesafe.akka" %% "akka-stream" % "2.6.19"

libraryDependencies += "org.scalactic" %% "scalactic" % "3.2.11"

libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.12-RC1" % "test"

libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "3.0.0"

libraryDependencies += "com.google.inject" % "guice" % "4.2.3"

libraryDependencies += ("net.codingwell" %% "scala-guice" % "5.0.2").cross(CrossVersion.for3Use2_13)

libraryDependencies += "com.typesafe.play" %% "play-json" % "2.10.0-RC5"

libraryDependencies += "org.scala-lang.modules" %% "scala-xml" % "2.0.1"

resolvers += Resolver.url("scoverage-bintray", url("https://dl.bintray.com/sksamuel/sbt-plugins/"))(Resolver.ivyStylePatterns)

这些是项目中的库

sbt: com.typesafe.akka:akka-actor-typed_3:2.6.19:jar
sbt: com.typesafe.akka:akka-actor_3:2.6.19:jar
sbt: com.typesafe.akka:akka-http-core_2.13:10.2.9:jar
sbt: com.typesafe.akka:akka-http_2.13:10.2.9:jar
sbt: com.typesafe.akka:akka-parsing_2.13:10.2.9:jar
sbt: com.typesafe.akka:akka-protobuf-v3_3:2.6.19:jar
sbt: com.typesafe.akka:akka-slf4j_3:2.6.19:jar
sbt: com.typesafe.akka:akka-stream_3:2.6.19:jar

尝试执行上述代码时,抛出以下异常:

Exception in thread "main" java.lang.NoSuchMethodError: 'void akka.actor.ExtensionId.$init$(akka.actor.ExtensionId)'
    at akka.http.scaladsl.Http$.<clinit>(Http.scala:845)
    at caro.aview.ViewAPI$.run(ViewAPI.scala:23)
    at caro.aview.run.main(ViewAPI.scala:14)

有人知道这是为什么吗?任何帮助将不胜感激,谢谢!

我也有这个问题。解决方案是使用 CrossVersion.for3Use2_13 选项将所有 akka 库移回 Scala 2.13,这与您在 akka-http 中使用的相同。看起来像是 scala 3 版本的 akka 中的错误。