简体   繁体   English

Scala SBT编译错误

[英]Scala SBT compile error

I have been following this tutorial to learn how to use Akka HTTP with Scala. 我一直在按照本教程学习如何在Scala中使用Akka HTTP。 I have no prior experience with Scala. 我没有使用Scala的经验。 I'm using IntelliJ Idea 2016.3 Ultimate. 我正在使用IntelliJ Idea 2016.3 Ultimate。

I created a project and configured 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"

I have the exactly same code as in the guide but when I run the SBT command compile I get the following error 我的代码与指南中的代码完全相同但是当我运行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

If I change my build.sbt to a more recent version of the akka libraries 如果我将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"
)

and compile again I get a different error 然后再次编译,我得到一个不同的错误

[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

How can I fix this? 我怎样才能解决这个问题? I don't know how to debug this error nor where to start tackling this problem. 我不知道如何调试此错误,也不知道从哪里开始解决此问题。 As I said, this is my first test with Scala so I don't have any prior experience with it, so, please, give me detail answers. 就像我说的那样,这是我第一次使用Scala进行测试,因此我以前没有使用它的经验,因此,请给我详细答案。

Thanks! 谢谢!

First error: Scala 2.9.x requires JVM runtime v 1.6 or 1.7, while you are using 1.8, which wouldn't work as we can clearly see from the message ('broken class' means that the runtime doesn't understand the structure of the files you are feeding it). 第一个错误:当您使用1.8时,Scala 2.9.x需要JVM运行时v 1.6或1.7,这将无法正常工作,因为我们从消息中可以清楚地看到(“破损类”意味着运行时不了解Java的结构)您正在馈送的文件)。

Second error requires a bit more context, could you post the code? 第二个错误需要更多上下文,您可以发布代码吗? I'm 99% sure you follow some outdated example, but use fresh library releases. 我99%肯定您会遵循一些过时的示例,但要使用新的库版本。 Eg there's no Duration in akka.util , no RoundRobinRouter in akka.routing etc. 例如,有没有Durationakka.util ,没有RoundRobinRouterakka.routing等。

I would suggest you to spend some time exploring/tweaking available samples , eg this one: https://github.com/akka/akka-samples/tree/master/akka-sample-main-scala . 我建议您花一些时间探索/调整可用的样本 ,例如: https : //github.com/akka/akka-samples/tree/master/akka-sample-main-scala

Also, note that akka evolves pretty fast, but has a solid documentation versioning support, so for each library release there's a separate documentation section on the site. 另外,请注意,akka的发展非常迅速,但具有可靠的文档版本控制支持,因此对于每个库发行版,网站上都有单独的文档部分。 You'll be notified about this at leas once a day when you visit an outdated release documentation page. 当您访问过时的发行文档页面时,每天都会至少有一次通知您。 Here's the link to he 2.4.17 docs: http://doc.akka.io/docs/akka/2.4.17/scala.html 这是指向他2.4.17文档的链接: http ://doc.akka.io/docs/akka/2.4.17/scala.html

There are 2 options: 有2个选项:

  1. Using java 7 with scala 2.7.2 将Java 7与Scala 2.7.2结合使用
  2. Using another version of scala (2.11.4), and java 8: 使用另一个版本的scala(2.11.4)和Java 8:

Example build.sbt 示例build.sbt

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM