如何让“sbt hello,world”运行?

时间:2017-03-09 23:50:16

标签: scala sbt

我正在试图了解Scala / SBT并且正在走过http://www.scala-sbt.org/0.13/docs/Hello.html

第一步进展顺利......我有一个名为“hello”的目录,其中包含$ sbt new sbt/scala-seed.g8的结果。

在第二步,“运行你的应用程序”,轮子脱落。

$ sbt
[info] Loading project definition from /Users/robert.kuhar/dev/Hello/project
[info] Set current project to Hello (in build file:/Users/robert.kuhar/dev/Hello/)
> run
[info] Compiling 1 Scala source to /Users/robert.kuhar/dev/Hello/target/scala-2.12/classes...
[info] 'compiler-interface' not yet compiled for Scala 2.12.1. Compiling...
[info] Resolving org.scala-sbt#interface;0.13.13 ...
[trace] Stack trace suppressed: run last compile:compileIncremental for the full output.
[error] (compile:compileIncremental) java.lang.reflect.InvocationTargetException
[error] Total time: 1 s, completed Mar 9, 2017 3:34:03 PM
>

run last compile:compileIncremental的输出对于noobie的眼睛是希腊的......

...
[info] Resolving org.scala-sbt#interface;0.13.13 ...
java.lang.VerifyError: Uninitialized object exists on backward branch 209
Exception Details:
  Location:
    scala/collection/immutable/HashMap$HashTrieMap.split()Lscala/collection/immutable/Seq; @249: goto
  Reason:
    Error exists in the bytecode
...

我离开赛道,在杂草中,寻找线索。

我不知道这个版本是否重要,但我是OSX Sierra的1.8 JDK和0.13.13 SBT。这台机器上有一个Scala 2.12.1。

$ sbt sbtVersion
[info] Loading project definition from /Users/robert.kuhar/dev/Hello/project
[info] Set current project to Hello (in build file:/Users/robert.kuhar/dev/Hello/)
[info] 0.13.13
$ java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)
$ scala -version
Scala code runner version 2.12.1 -- Copyright 2002-2016, LAMP/EPFL and Lightbend, Inc.

如何在我的开发箱上运行“SBT Hello,World”?

1 个答案:

答案 0 :(得分:2)

这是一种没有sbt new插件的方法。

在目录Hello中创建以下目录和文件:

├── build.sbt
└── src
    └── main
        └── scala
            └── Hello.scala

将其放入Hello.scala

object Hello extends App{
  println("Hello")
}

Hello/build.sbt中添加以下行:

scalaVersion := "2.11.8

使用run

运行它
/tmp/Hello  $ sbt                                                                                                                                                                                                                                                                                                                                              [16:40:38]
[info] Loading global plugins from /home/brian/.sbt/0.13/plugins
[info] Set current project to hello (in build file:/tmp/Hello/)
> run
[info] Compiling 1 Scala source to /tmp/Hello/target/scala-2.11/classes...
[info] Running Hello 
Hello
...

过去我使用g8 / giterate模板获得了“混合”结果,但这可能是您的问题,也可能不是。我使用了https://github.com/sbt/sbt-fresh,效果很好。如果您对Scala和SBT全新,那么上面的代码和构建设置应该可以帮助您入门。