Gary Sieling

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")
  }
}
Exit mobile version