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")
  }
}

5 Replies to “Fixing Scala Error: Main method is not static in class X”

  1. Is it possible to use a Main class and also use Dependency Injection? I’m building a small app and I want to inject services, appConfigs etc.
    Thank you in advance for your help 🙂

Leave a Reply

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