score:9
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 compilera 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
Source: stackoverflow.com
Related Query
- Main method is not called in Scala script
- In Scala static value initialization does not appear to be happening before the main method is called
- Scala IDE Error: Main method not found in class 'hello'
- not found: type in main method scala
- Main method works for java, but not scala
- Error in running Scala Program: Main method not found in class main, please define the main method
- When and why does Scala code needs to be on a method called main inside an object?
- scala Ide Not identifying my class as main method
- Spark Scala Error - Error: Main method not found in class
- Difference between using App trait and main method in scala
- scala slick method I can not understand so far
- Main method in Scala
- Could not find or load main class in scala in intellij IDE
- reduceByKey method not being found in Scala Spark
- Scala project won't compile in Eclipse; "Could not find the main class."
- Scala App val initialization in main method
- Scala Error: Could not find or load main class in both Scala IDE and Eclipse
- How to call main method of a Scala program from the main method of a java program?
- "Could not find main method from given launch configuration" when using Java+Scala+Slick2D
- How to declare scala method so that it can be called from Java using varargs style
- Terminal cd command not working from Scala script
- Scala is not inferring the appropriate method
- Diffrence between extends from App and object contain main method in scala
- Why does Scala starts from an object's main method instead of a class's static main method?
- Scala case class is not matched in receive method (in akka actors)
- Scala IDE Error -could not find/load main class
- Executing Scala objects in Eclipse without a main method
- Could not find implicit when used in main method
- Scala Main Class not found in Eclipse ide
- Scala - IntelliJ IDEA Error: Could not find or load main class
More Query from same tag
- Play Framework 2.0 correct way to represent a set in a query using Anorm
- nested Annotation List in Scala
- Comparing Scala reflection Symbols
- Pass in custom predicate to TableQuery's filter method
- Value not found
- Scala Map from Test File
- How to debug and fix Diverging implicits
- Apache spark - loading data from elasticsearch is too slow
- Resolve two sbt imports with the same classes
- Spark 2.0.1: split JSON Array Column into ArrayType(StringType)
- Apache Spark Task Serialization
- Scala Reflection to get the types of all fields of an object
- How can Akka Alppaka Producer Integration with Akka HTTP
- How to Spark implement the interactive In-Memory Cache?
- Why does function expecting type alias input does not typechecks if I pass undelying type?
- Scala Slick: MappedColumnType cannot find implicit value for BaseColumlnType[String]
- Get hostname and pid in scala without using java classes?
- Working with Streams wrapped in Options
- Accessing Scala instance variables from Java in Eclipse
- Akka flow using fold not completing
- Slick left/right/outer joins with Option
- Need help Parsing strange JSON with scala
- sbt android won't run android:package on example project multiproject-same-dependencies because of libraryProject errors
- Null checks for scala XML attributes
- simple video decoding in Java / Scala
- Fail-safe wrapper for spary-json parseJson func
- [Exception: DB plugin is not registered.]
- java.io.InterruptedIOException when invoking next() over a ResultScanner object
- Why does this public field have a PRIVATE flag?
- Typesafe/Hocon config: variable substitution: reference path