Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a Scala method from command line?

Tags:

console

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
like image 699
John Threepwood Avatar asked Aug 14 '12 09:08

John Threepwood


2 Answers

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.

like image 103
Daniel C. Sobral Avatar answered Sep 28 '22 04:09

Daniel C. Sobral


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

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.

like image 27
Sean Parsons Avatar answered Sep 28 '22 03:09

Sean Parsons