如何从命令行启动 Scala 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11949764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:26:32  来源:igfitidea点击:

How to start a Scala method from command line?

scalaconsole

提问by John Threepwood

This question may sound a bit stupid, but I couldn't figure out, how to start a Scala method from the command line.

这个问题可能听起来有点愚蠢,但我想不通,如何从命令行启动 Scala 方法。

I compiled the following file Test.scala:

我编译了以下文件Test.scala

package example

object Test {
  def print() {
    println("Hello World")
  }

}

}

with scalac Test.scala.

scalac Test.scala.

Then, I can run the method printwith scalain two steps:

然后,我可以运行的方法printscala两个步骤:

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

But what I really like to do is, to run the method directly from the command line with one command like scala example.Test.print.

但我真正喜欢做的是,直接从命令行使用一个命令(如scala example.Test.print.

How can I achieve this goal ?

我怎样才能实现这个目标?

UPDATE:Suggested solution by ArikG does not work for me - What I am missing ?

更新: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

where

在哪里

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

UPDATE 2 - Possible SOLUTIONs:

更新 2 - 可能的解决方案:

  • As ArikG correctly suggested, with scala -e "import example.Test._; print"works well with Windows 7.
  • See answer of Daniel to get it work without the import statement
  • 正如 ArikG 正确建议的那样,withscala -e "import example.Test._; print"在 Windows 7上运行良好。
  • 请参阅 Daniel 的回答以使其在没有导入语句的情况下工作

采纳答案by Daniel C. Sobral

Let me expand on this solution a bit:

让我稍微扩展一下这个解决方案:

scala -e 'example.Test.print'

Instead, try:

相反,请尝试:

scala -cp path-to-the-target-directory -e 'example.Test.print'

Where the target directory is the directory where scala used as destination for whatever it compiled. In your example, it is notC:\Users\John\Scala\Examples\example, but C:\Users\John\Scala\Examples. The directory exampleis where Scala will look for classes belonging to the packageexample.

其中目标目录是 scala 用作其编译目标的目录。在您的示例中,它不是C:\Users\John\Scala\Examples\example,而是C:\Users\John\Scala\Examples。该目录example是 Scala 查找属于包的类的地方example

This is why things did not work: it expected to find the package exampleunder a directory example, but there were no such directory under the current directory in which you ran scala, and the classfiles that were present on the current directory were expected to be on the default package.

这就是为什么事情不起作用:它希望example在目录示例下找到包,但是在您运行的当前目录下没有这样的目录,并且当前目录中scala存在的类文件应该在默认包。

回答by Sean Parsons

The best way to do this is to extend Appwhich is a slightly special class (or at least DelayedInit which underlies it is):

最好的方法是扩展App,它是一个稍微特殊的类(或者至少是它的基础的 DelayedInit ):

package example

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

It's still possible to add methods to this as well, the body of the object is executed on startup.

仍然可以为此添加方法,对象的主体在启动时执行。

回答by Arik G

Here you go:

干得好:

scala -e 'example.Test.print'