How to launch a Scala application with an object (main, app)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 6.4, “How to launch a Scala application with an object.”

Problem

You want to start a Scala application with a main method, or provide the entry point for a script.

Solution

There are two ways to create a launching point for your application: define an object that extends the App trait, or define an object with a properly defined main method.

For the first solution, define an object that extends the App trait. Using this approach, the following code creates a simple but complete Scala application:

object Hello extends App {
    println("Hello, world")
}

The code in the body of the object is automatically run, just as if it were inside a main method.

Just save that code to a file named Hello.scala, compile it with scalac, and then run it with scala, like this:

$ scalac Hello.scala

$ scala Hello
Hello, world

When using this approach, any command-line arguments to your application are implicitly available through an args object, which is inherited from the App trait. The args object is an instance of Array[String], just as if you had declared a main method yourself. The following code demonstrates how to use the args object:

object Hello extends App {
    if (args.length == 1)
        println(s"Hello, ${args(0)}")
    else
        println("I didn't get your name.")
}

After it’s been compiled, this program yields the following results:

$ scala Hello
I didn't get your name.

$ scala Hello Joe
Hello, Joe

The second approach to launching an application is to manually implement a main method with the correct signature in an object, in a manner similar to Java:

object Hello2 {
    def main(args: Array[String]) {
        println("Hello, world")
    }
}

This is also a simple but complete application.

Discussion

Note that in both cases, Scala applications are launched from an object, not a class. I tend to use the App trait for both scripts and larger applications, but you can use either approach. I recommend reviewing the source code for the App trait to better understand what it performs. The source code is available from the URL in the See Also section.

The Scaladoc for the App trait currently includes two caveats:

  1. It should be noted that this trait is implemented using the DelayedInit functionality, which means that fields of the object will not have been initialized before the main method has been executed.
  2. It should also be noted that the main method will not normally need to be overridden: the purpose is to turn the whole class body into the “main method.” You should only choose to override it if you know what you are doing.

See the Scaladoc for the App and DelayedInit traits for more information.

See Also

The Scala Cookbook

This tutorial is sponsored by the Scala Cookbook, which I wrote for O’Reilly:

You can find the Scala Cookbook at these locations:

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.