如何运行使用Simple Build Tool创建的包?

时间:2010-05-22 00:31:19

标签: scala jar sbt

我跑:

$ echo 'object Hi { def main(args: Array[String]) { println("Hi!") } }' > hw.scala
$ sbt
> warn
Set log level to warn
> run
Hi!
> package
$ java -jar target/scala_2.7.7/test_2.7.7-1.0.jar 
Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)

为什么我不能这样运行这个jar包?

2 个答案:

答案 0 :(得分:4)

您正在尝试在没有scala运行时的情况下运行它。使用scala可执行文件:

scala -jar target/scala_2.7.7/test_2.7.7-1.0.jar 

或者,将scala jar添加到类路径

java -cp target/scala_2.7.7/test_2.7.7-1.0.jar:$PATH_TO_SCALA_JAR Hi

答案 1 :(得分:0)

我不知道您使用的是哪个版本的sbt,或者您使用的是哪个项目设置,但通常您的hw.scala文件应该放在src / main / scala目录中供sbt查找。另外,正如synesso所说,scala运行时似乎不存在。通常,sbt会在创建新项目时下载这些内容。 我刚刚在一个fres项目中使用sbt 0.7.3尝试过它,这个工作原理:

$ sbt
Project does not exist, create new project? (y/N/s) y
Name: test
Organization: test
Version [1.0]: 
Scala version [2.7.7]: 
sbt version [0.7.3]: 
Getting Scala 2.7.7 ...
:: retrieving :: org.scala-tools.sbt#boot-scala
    confs: [default]
    2 artifacts copied, 0 already retrieved (9911kB/26ms)
Getting org.scala-tools.sbt sbt_2.7.7 0.7.3 ...
:: retrieving :: org.scala-tools.sbt#boot-app
    confs: [default]
    15 artifacts copied, 0 already retrieved (4023kB/25ms)
[success] Successfully initialized directory structure.
[info] Building project test 1.0 against Scala 2.7.7
[info]    using sbt.DefaultProject with sbt 0.7.3 and Scala 2.7.7

> exit

$echo 'object Hi { def main(args: Array[String]) { println("Hi!") } }' > src/main/scala/hw.scala

> sbt
[info] Building project test 1.0 against Scala 2.7.7
[info]    using sbt.DefaultProject with sbt 0.7.3 and Scala 2.7.7
> run
Hi!

> package
[info] 
[info] == compile ==
[info]   Source analysis: 0 new/modified, 0 indirectly invalidated, 0 removed.
[info] Compiling main sources...
[info] Nothing to compile.
[info]   Post-analysis: 2 classes.
[info] == compile ==
[info] 
[info] == package ==
[info] == package ==
[success] Successful.