Search code examples
scalaprogram-entry-point

scala object extends app does not print anything


I am learning Scala and as part of the journey I had come across two different ways to write your scala class - one with a main method and other without the main method but by extending App (earlier Application is deprecated due to concurrency issues).

I am executing the scripts in Command Line via scala executable as scala <nameOfScript>.scala. I run Scala 2.11.7 in Windows.

I have no issues when running a scala script/class with a main method.

object ObjectWithMainMethod {
    def main(args:Array[String]) = {
            println("Object with a main() method executed..")
    }
}

It produces the following output.

Object with a main() method executed..

But, I don't get the output with its counterpart, which is extending App trait but without the main method.

object AppWithoutMainMethod extends App {
    println("AppWithout main() method executed")
}

When I run this scala script, it does not print anything. However, when I looked at the complied .class files via javap utility, I could see the PSVM (public static void main() method) inside.

Am I missing something? Any help on this will be highly appreciated.


Solution

  • The same thing works if I run the file without the .scala extension. I am not sure the reason behind this.

    scala AppWithoutMainMethod
    

    It produces the following output

    AppWithout main() method executed