【问题标题】:Scala SBT compile errorScala SBT 编译错误
【发布时间】:2017-03-22 14:54:21
【问题描述】:

我一直在关注this tutorial 以了解如何将 Akka HTTP 与 Scala 一起使用。我之前没有使用 Scala 的经验。我正在使用 IntelliJ Idea 2016.3 Ultimate。

我创建了一个项目并配置了as the guide says

name := "My Project"

version := "1.0"

scalaVersion := "2.9.1"

resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies += "com.typesafe.akka" % "akka-actor" % "2.0"

我有完全相同的代码as in the guide,但是当我运行 SBT 命令编译时出现以下错误

[info] Updating {file:/Users/Javyer/Testing/Akka-Test/}akka-test...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/Javyer/Testing/Akka-Test/target/scala-2.9.1/classes...
[info] 'compiler-interface' not yet compiled for Scala 2.9.1.final. Compiling...
error: error while loading CharSequence, class file '/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar(java/lang/CharSequence.class)' is broken
(bad constant pool tag 18 at byte 10)
error: error while loading AnnotatedElement, class file '/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar(java/lang/reflect/AnnotatedElement.class)' is broken
(bad constant pool tag 18 at byte 76)
error: error while loading Arrays, class file '/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar(java/util/Arrays.class)' is broken
(bad constant pool tag 18 at byte 765)
error: error while loading Comparator, class file '/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar(java/util/Comparator.class)' is broken
(bad constant pool tag 18 at byte 20)
/var/folders/kw/80c0kgzs0b9d06vjrbx0l6g40000gn/T/sbt_139df807/xsbt/ExtractAPI.scala:549: error: java.util.Comparator does not take type parameters
  private[this] val sortClasses = new Comparator[Symbol] {
                                      ^
5 errors found
[info] Resolving org.scala-sbt#interface;0.13.13 ...
[error] (compile:compileIncremental) Error compiling sbt component 'compiler-interface'
[error] Total time: 5 s, completed Mar 22, 2017 11:42:21 AM

如果我将 build.sbt 更改为 akka 库的更新版本

name := "Akka-Test"

version := "1.0"

scalaVersion := "2.12.1"

resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.4.17",
  "com.typesafe.akka" %% "akka-remote" % "2.4.17"
)

再次编译我得到一个不同的错误

[info] Updating {file:/Users/Javyer/Testing/Akka-Test/}akka-test...
[info] Resolving jline#jline;2.14.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/Javyer/Testing/Akka-Test/target/scala-2.12/classes...
[error] /Users/Javyer/Testing/Akka-Test/src/main/scala/Pi.scala:5: object RoundRobinRouter is not a member of package akka.routing
[error] import akka.routing.RoundRobinRouter
[error]        ^
[error] /Users/Javyer/Testing/Akka-Test/src/main/scala/Pi.scala:6: object Duration is not a member of package akka.util
[error] import akka.util.Duration
[error]        ^
[error] /Users/Javyer/Testing/Akka-Test/src/main/scala/Pi.scala:7: object duration is not a member of package akka.util
[error] import akka.util.duration._
[error]                  ^
[error] /Users/Javyer/Testing/Akka-Test/src/main/scala/Pi.scala:17: not found: type Duration
[error]   case class PiApproximation(pi: Double, duration: Duration)
[error]                                                    ^
[error] /Users/Javyer/Testing/Akka-Test/src/main/scala/Pi.scala:40: not found: value RoundRobinRouter
[error]     val workerRouter = context.actorOf(Props[Worker].withRouter(RoundRobinRouter(nrOfWorkers)), name = "workerRouter")
[error]                                                                 ^
[error] 5 errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 4 s, completed Mar 22, 2017 11:47:47 AM

我该如何解决这个问题?我不知道如何调试这个错误,也不知道从哪里开始解决这个问题。正如我所说,这是我对 Scala 的第一次测试,所以我之前没有任何经验,所以请给我详细的答案。

谢谢!

【问题讨论】:

    标签: scala intellij-idea sbt akka-http


    【解决方案1】:

    第一个错误:Scala 2.9.x 需要 JVM 运行时 v 1.6 或 1.7,而您使用的是 1.8,从消息中我们可以清楚地看到,这不起作用('broken class' 意味着运行时不理解您提供给它的文件的结构)。

    第二个错误需要更多的上下文,你能发布代码吗?我 99% 确定您遵循一些过时的示例,但使用新的库版本。例如。在akka.util 中没有Duration,在akka.routing 中没有RoundRobinRouter 等等。

    我建议您花一些时间探索/调整 available samples,例如这个:https://github.com/akka/akka-samples/tree/master/akka-sample-main-scala.

    另外,请注意,akka 发展得非常快,但具有可靠的文档版本控制支持,因此对于每个库版本,网站上都有一个单独的文档部分。当您访问过时的发布文档页面时,您每天至少会收到一次通知。这是他 2.4.17 文档的链接:http://doc.akka.io/docs/akka/2.4.17/scala.html

    【讨论】:

    • 我有来自this link的完全相同的代码检查上面的代码作为命令行应用程序运行标题。整个 Pi.scala 类。
    • 没错。您使用 Akka 2.0 和 akka 2.4.17 的示例。从那时起,API 发生了很大变化。您绝对应该使用示例中更新的代码版本。查看我之前提供的链接。
    【解决方案2】:

    有两种选择:

    1. 在 Scala 2.7.2 中使用 java 7
    2. 使用另一个版本的 scala (2.11.4) 和 java 8:

    build.sbt 示例

    scalaVersion := Option(System.getProperty("scala.version")).getOrElse("2.11.4")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-24
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 2011-11-12
      相关资源
      最近更新 更多