【发布时间】:2012-08-16 00:27:55
【问题描述】:
这个问题可能听起来有点愚蠢,但我不知道如何从命令行启动 Scala 方法。
我编译了以下文件Test.scala:
package example
object Test {
def print() {
println("Hello World")
}
}
scalac Test.scala.
然后,我可以分两步运行print 和scala 方法:
C:\Users\John\Scala\Examples>scala
Welcome to Scala version 2.9.2 (Java HotSpot(TM) Client VM, Java 1.6.0_32).
Type in expressions to have them evaluated.
Type :help for more information.
scala> example.Test.print
Hello World
但我真正喜欢做的是,使用scala example.Test.print 之类的命令直接从命令行运行该方法。
我怎样才能实现这个目标?
更新: ArikG 建议的解决方案对我不起作用 - 我缺少什么?
C:\Users\John\Scala\Examples>scala -e 'example.Test.print'
C:\Users\John\AppData\Local\Temp\scalacmd1874056752498579477.scala:1: error: u
nclosed character literal
'example.Test.print'
^
one error found
C:\Users\John\Scala\Examples>scala -e "example.Test.print"
C:\Users\John\AppData\Local\Temp\scalacmd1889443681948722298.scala:1: error: o
bject Test in package example cannot be accessed in package example
example.Test.print
^
one error found
在哪里
C:\Users\John\Scala\Examples>dir example
Volume in drive C has no label.
Volume Serial Number is 4C49-8C7F
Directory of C:\Users\John\Scala\Examples\example
14.08.2012 12:14 <DIR> .
14.08.2012 12:14 <DIR> ..
14.08.2012 12:14 493 Test$.class
14.08.2012 12:14 530 Test.class
2 File(s) 1.023 bytes
2 Dir(s) 107.935.760.384 bytes free
更新 2 - 可能的解决方案:
- 正如 ArikG 正确建议的那样,
scala -e "import example.Test._; print"在 Windows 7 上运行良好。 - 查看 Daniel 的回答以使其在没有 import 语句的情况下工作
【问题讨论】:
-
scala -e ...在我的 Mac 上运行良好。嗯……scala -e "import example._; example.Test.print"呢? -
@ArikG 你明白了。这适用于我的 Windows 7 安装程序。谢谢。