我有以下hello.scala
文件:
object hello {
def main(args: Array[String]): Unit = {
println("Hello World!")
}
}
使用scalac
进行编译后,我会回来hello.class
和hello$.class
。
如何使用java
命令执行它?我应该把???
放在哪里?
$ java ??? hello
答案 0 :(得分:2)
使用scala
命令运行它。即scala hello
答案 1 :(得分:2)
您正在寻找
java -cp /wherever/my/scala/lives/lib/scala-library.jar hello
或者你可以
scala hello
答案 2 :(得分:1)
我认为可能是时候向用户介绍另一种可以轻松开发Scala应用程序的工具 - 编译源代码,构建具有所有依赖关系的单jar工件等。我不认为单个object hello
应用程序是OP在Scala中开发的最后一个词,也是最终需要的工具。
话虽如此,我强烈建议使用项目构建工具并且sbt可能非常合适。
从the sbt project's web site下载sbt启动程序,并在sbt
对象所在的目录中运行hello
。
要运行hello
对象,请执行sbt
并让它执行其他命令 - 其中run
是运行hello
对象应用程序的命令。
jacek:~/sandbox/so/hello-object
$ sbt
[info] Loading global plugins from /Users/jacek/.sbt/0.13/plugins
[info] Updating {file:/Users/jacek/.sbt/0.13/plugins/}global-plugins...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to hello-object (in build file:/Users/jacek/sandbox/so/hello-object/)
[hello-object]> run
[info] Updating {file:/Users/jacek/sandbox/so/hello-object/}hello-object...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/jacek/sandbox/so/hello-object/target/scala-2.10/classes...
[info] Running hello
Hello World!
[success] Total time: 12 s, completed Mar 16, 2014 2:32:39 PM
如果您需要知道run
hello
对象的命令行到底是什么,请执行last
或last run
以明确并打印部件出你的。
[hello-object]> last
[debug]
[debug] Initial source changes:
[debug] removed:Set()
[debug] added: Set()
[debug] modified: Set()
[debug] Removed products: Set()
[debug] External API changes: API Changes: Set()
[debug] Modified binary dependencies: Set()
[debug] Initial directly invalidated sources: Set()
[debug]
[debug] Sources indirectly invalidated by:
[debug] product: Set()
[debug] binary dep: Set()
[debug] external source: Set()
[debug] All initially invalidated sources: Set()
[debug] Copy resource mappings:
[debug]
[info] Running hello
[debug] Waiting for threads to exit or System.exit to be called.
[debug] Classpath:
[debug] /Users/jacek/sandbox/so/hello-object/target/scala-2.10/classes
[debug] /Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar
[debug] Waiting for thread run-main-3 to terminate.
[debug] Thread run-main-3 exited.
[debug] Interrupting remaining threads (should be all daemons).
[debug] Sandboxed run complete..
[debug] Exited with code 0
[success] Total time: 0 s, completed Mar 16, 2014 2:41:57 PM
[debug] > shell
Classpath
之后的Running hello
部分是您需要的 - 运行hello
对象的类路径。
jacek:~/sandbox/so/hello-object
$ java -cp /Users/jacek/sandbox/so/hello-object/target/scala-2.10/classes:/Users/jacek/.sbt/boot/scala-2.10.3/lib/scala-library.jar hello
Hello World!
一旦您对Scala开发的结果感到满意并且应用程序已准备好进行生产部署,请执行package
为其他项目创建一个jar文件(甚至是Java项目)。< / p>
[hello-object]> package
[info] Packaging /Users/jacek/sandbox/so/hello-object/target/scala-2.10/hello-object_2.10-0.1-SNAPSHOT.jar ...
[info] Done packaging.
[success] Total time: 1 s, completed Mar 16, 2014 2:34:34 PM
您可能希望将其发布到Ivy2(默认)或带有publishLocal
的Maven存储库,以便其他Scala / Java / JRuby / Groovy项目可以使用它。
[hello-object]> publishLocal
[info] Packaging /Users/jacek/sandbox/so/hello-object/target/scala-2.10/hello-object_2.10-0.1-SNAPSHOT-sources.jar ...
[info] Done packaging.
[info] Wrote /Users/jacek/sandbox/so/hello-object/target/scala-2.10/hello-object_2.10-0.1-SNAPSHOT.pom
[info] :: delivering :: default#hello-object_2.10;0.1-SNAPSHOT :: 0.1-SNAPSHOT :: integration :: Sun Mar 16 14:35:57 CET 2014
[info] delivering ivy file to /Users/jacek/sandbox/so/hello-object/target/scala-2.10/ivy-0.1-SNAPSHOT.xml
[info] Main Scala API documentation to /Users/jacek/sandbox/so/hello-object/target/scala-2.10/api...
model contains 2 documentable templates
[info] Main Scala API documentation successful.
[info] Packaging /Users/jacek/sandbox/so/hello-object/target/scala-2.10/hello-object_2.10-0.1-SNAPSHOT-javadoc.jar ...
[info] Done packaging.
[info] published hello-object_2.10 to /Users/jacek/.ivy2/local/default/hello-object_2.10/0.1-SNAPSHOT/poms/hello-object_2.10.pom
[info] published hello-object_2.10 to /Users/jacek/.ivy2/local/default/hello-object_2.10/0.1-SNAPSHOT/jars/hello-object_2.10.jar
[info] published hello-object_2.10 to /Users/jacek/.ivy2/local/default/hello-object_2.10/0.1-SNAPSHOT/srcs/hello-object_2.10-sources.jar
[info] published hello-object_2.10 to /Users/jacek/.ivy2/local/default/hello-object_2.10/0.1-SNAPSHOT/docs/hello-object_2.10-javadoc.jar
[info] published ivy to /Users/jacek/.ivy2/local/default/hello-object_2.10/0.1-SNAPSHOT/ivys/ivy.xml
[success] Total time: 4 s, completed Mar 16, 2014 2:36:01 PM
最后但并非最不重要的是 - 让一个包含所有依赖项的jar使用sbt-assembly插件。
答案 3 :(得分:0)
另一种方法是,调用例如hello.sh
的文件,将其设置为可执行文件,
chmod +x hello.sh
注意文件的标题,
#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#
object hello {
def main(args: Array[String]): Unit = {
println("Hello World!")
}
}
使用
简单地运行它./hello.sh
开关-savecompiled
在第一次执行时创建一个JAR文件,执行后启动会更快,因为不需要(重新)编译脚本代码。