How to start a Scala method from command line?

Viewed 16830

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
3 Answers
Related