Fixing Scala Error: Main method is not static in class X

If you set up a Scala HelloWorld example naively, it is easy to get the following perplexing error message:

:runError: Main method is not static in class com.garysieling.HelloWorld, please define the main method as:
   public static void main(String[] args)

This happens if the code looks like the following:

package com.garysieling {
  class HelloWorld extends App {
     System.out.println("test")
  }

In fact, “HelloWorld” must be set as an object, as there are no statics with classes in scala.

package com.garysieling {
  import scala.App
 
  object HelloWorld extends App {
     System.out.println("test")
  }
}

Interested in Scala? I send out weekly, personalized emails with articles and conference talks. Click here to see an example and subscribe.

4 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *