Search code examples
scalasbtscala-2.12

main method not executed with App in Scala


Hi I'm new to scala and trying to use scala 2.12.10 for a project. I'm trying to follow along with this tutorial which is written for the latest version of scala and I've gotten it to work fine (the print statement prints to sbt shell as expected). But when I try to do this in 2.12.10, it seems to run successfully but does not print anything. I found a post suggesting to run Console.out.flush after the print statements but this did not work for me. Any thoughts on how to get the print statements to render. If you try to implement the tutorial, I had to adjust the object to the following so that it would run:

package example

object Main extends App {
  def main(): Unit = {
    println("here")
    val ages: Seq[Int] = Seq(42, 61, 29, 64)
    println("here ")
    //println(s"The oldest person is ${ages.max}")
  }
}

I'm wondering if adding extends App is screwing this up but it was the only thing that worked to get it to run without throwing an error.


Solution

  • In order for a scala app to be runnable you either need an object that has a method with following signature:

     def main(args: Array[String]): Unit
    

    or the object needs to extends the App trait.

    But not both.