如何从命令行启动Scala方法? [英] How to start a Scala method from command line?

查看:843
本文介绍了如何从命令行启动Scala方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我编译了以下文件 Test.scala

 包示例

对象测试{
def print(){
println( H​​ello World)
}

}



具有 scalac Test.scala



然后,我可以通过两个步骤运行 scala 的方法 print

  C:\Users\John\Scala\Examples> scala 
欢迎使用Scala 2.9.2版( Java HotSpot(TM)客户端VM,Java 1.6.0_32)。
输入表达式以对其求值。
类型:帮助以获取更多信息。

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:错误:u
封闭字符字面量
'example.Test.print'
^
发现一个错误

C:\Users\John\Scala\Examples> scala -e example.Test.print
C:\Users \John\AppData\Local\Temp\scalacmd1889443681948722298.scala:1:错误:o
bject不能在包示例
example.Test.print $ b $中访问包示例中的测试。 b ^
发现一个错误

其中

  C:\Users\John\Scala\Examples> dir example 
驱动器C中的卷没有标签。
卷序列号为4C49-8C7F

C目录:Directory用户\John\Scala\示例\示例

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文件1.023字节
2目录)107.935.760.384字节可用

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




  • 如ArikG正确建议的那样,使用 scala -e import example.Test._; print 在Windows 7上运行良好。

  • 请参见Daniel的回答以使其在不使用导入语句的情况下正常工作


解决方案

让我对该解决方案进行一些扩展:

  scala -e 'example.Test.print'

相反,请尝试:

  scala -cp到目标目录的路径-e'example.Test.print'

目标目录是scala用作编译目标的目录。在您的示例中,不是 C:\Users\John\Scala\Examples\example ,但 C:\用户\John\Scala\示例。目录 example 是Scala在其中查找属于 package example 的类的地方。 / p>

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


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

I compiled the following file Test.scala :

package example

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

}

with scalac Test.scala.

Then, I can run the method print with scala in two steps:

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.

How can I achieve this goal ?

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

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:

  • 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

解决方案

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 not C:\Users\John\Scala\Examples\example, but C:\Users\John\Scala\Examples. The directory example is where Scala will look for classes belonging to the package example.

This is why things did not work: it expected to find the package example under 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.

这篇关于如何从命令行启动Scala方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆