score:9

Accepted answer

as shown in getting started with scala, you should explicitly invoke the main method, when running scala files as a script:

class word {
}

object helloworld {
  def main(args: array[string]) {
    println("hello, world!")
  }
}

helloworld.main(args)

if you want to compile and run, you'll have to give the name of the object holding the main method:

scalac test.scala
scala helloworld

score:0

if you’re on some flavor of unix, you can run a scala script as a shell script by prepending a pound bang directive at the top of the file.

for example, type the following into a file named helloarg:

#!/bin/sh
exec scala "$0" "$@"
!#
// say hello to the first argument
println("hello, "+ args(0) +"!")

the initial #!/bin/sh must be the very first line in the file.
once you set its execute permission:

$ chmod +x helloarg

you can run the scala script as a shell script by simply saying:

$ ./helloarg globe

if you’re on windows, you can achieve the same effect by naming the file helloarg.bat and placing this at the top of your script:

::#!
@echo off
call scala % 0 % *
goto :eof
::!#

score:5

actually what you wrote is not a scala script, but a simple scala program. scala scripts differs from simple programs in the way they are organized and run, there is no compilation stage, scala compiler will interpret source file line by line, like in the repl session.

to make a scala script from your file just leave:

println("this is a scala script") 

and save it in *.scala file, then run it with scala command in the terminal window without any previous scalac call. scala compiler will automatically determines that this is a script, because source file ends with an expression. when you place helloworld.main(args) at the end of your file, it will be executed as a script file, but the class and companion object with main() method are completely obsolete

score:9

the reason of this strange behavior is simple. when running the scala executable with a script name passed on the command line (as in scala hello.scala), what happens by default is that it will try to "guess" what to run. if the file contains only a single object with a suitable main method (meaning that it would also be a proper program entry point), that main method is run. otherwise, the scala file is interpreted as a proper script (with statements accepted at the top level and run line by line, like in the interactive mode). so there really are 2 ways of running a scala file using the scala interpreter executable (the first one beig seldom used).

so as soon as you added another class to you source file, the first mode of execution (calling the main method) did not trigger anymore and you falled back to standard line by line interpretation. thus your main method is not called anymore unless you explicitly call it.

for reference, here is the output of scala -help (emphasis mine):

usage: scala <options> [<script|class|object|jar> <arguments>] or scala -help

all options to scalac (see scalac -help) are also allowed. the first given argument other than options to scala designates what to run. runnable targets are:

  • a file containing scala source
  • the name of a compiled class
  • a runnable jar file with a valid main-class attribute
  • or if no argument is given, the repl (interactive shell) is started

options to scala which reach the java runtime:

-dname=prop passed directly to java to set system properties
-j -j is stripped and passed to java as-is
-nobootcp do not put the scala jars on the boot classpath (slower)

other startup options:

-howtorun what to run (default: guess)
-i preload before starting the repl
-e execute as if entered in the repl
-save save the compiled script in a jar for future use
-nc no compilation daemon: do not use the fsc offline compiler

a file argument will be run as a scala script unless it contains only self-contained compilation units (classes and objects) and exactly one runnable main method. in that case the file will be compiled and the main method invoked. this provides a bridge between scripts and standard scala source.

options for plugin 'continuations':
-p:continuations:enable enable continuations


Related Query

More Query from same tag