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

发布于 2024-09-02 16:27:57 字数 593 浏览 6 评论 0原文

我运行:

$ 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包呢?

I run:

$ 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)

Why can't I run this jar package this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错爱 2024-09-09 16:27:57

您正在尝试在没有 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

You are attempting to run it without the scala runtime. Either use the scala executable:

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

or, add the scala jar to the classpath

java -cp target/scala_2.7.7/test_2.7.7-1.0.jar:$PATH_TO_SCALA_JAR Hi
冷血 2024-09-09 16:27:57

我不知道你正在使用哪个版本的 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.

I don't know which version of sbt you're using, or what project setup you have used, but normally your hw.scala file should be placed in src/main/scala directory for sbt to find it. Also, as synesso remarked, the scala runtime seems to be absent. Normally sbt will just download these when creating a new project.
I just tried it using sbt 0.7.3 in a fres project, and this works:

$ 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文