0

If I use like below considering I don't need to take arguments, it doesn't detect for Scala in eclipse.

object HelloWorld {
  def main(): Unit = {
    println("Hello Scala!!!")
  }
}

It works fine with args: Array[String]

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello Scala!!!")
  }
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
PKM15
  • 109
  • 1
  • 8
  • 2
    http://stackoverflow.com/questions/10783190/why-does-main-method-in-java-always-need-arguments – Michael Zajac Jan 26 '16 at 18:37
  • 1
    Define "compulsory"; You can *have* an arg-less `main`, it just won't be considered an entry point. (Scala may insist on a single main method with the conventional args; I don't know. But it wouldn't be an entry point without the args.) – Dave Newton Jan 26 '16 at 18:38

1 Answers1

6

Well it's simply a convention on the JVM. You won't be able to invoke your object as entry point when running your program. For example, in Scala.js you have main() without arguments.

If you don't need the arguments you can mixin the App trait:

object HelloWorld extends App {
  println("Hello Scala!!!")
}
0__
  • 66,707
  • 21
  • 171
  • 266
  • 3
    you can still get arguments with the App trait: http://stackoverflow.com/q/11667630/217324 – Nathan Hughes Jan 26 '16 at 18:42
  • @NathanHughes yes that is correct. They are available through value `args` as before. – 0__ Jan 26 '16 at 18:45
  • Thanks & Great to know. Tutorial I was following mentioned that you don't have to write all what JVM requires Scala will compile for you. I guess it is true in this case also :) – PKM15 Jan 26 '16 at 18:54
  • Worth adding, `def main(args: Array[String]): Int = ???` will warn and isn't runnable from IDE, but `scala` runner will run it. It would be nice if the scala runner relaxed more such conventions. I'd like to `def main(args: String*)` for instance and have the runner just use it, even if Java-oriented launchers balk. – som-snytt Jan 26 '16 at 21:13