This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.9, “Specifying a Main Class to Run with SBT.”
In a Scala SBT project, you have multiple
main methods in objects in your project, and you want to specify which
main method should be run when you type
sbt run, or specify the
main method that should be invoked when your project is packaged as a JAR file.
If you have multiple
main methods in your project and want to specify which
main method to run when typing
sbt run, add a line like this to your build.sbt file:
// set the main class for 'sbt run' mainClass in (Compile, run) := Some("com.alvinalexander.Foo")
This class can either contain a
main method, or extend the
To specify the class that will be added to the manifest when your application is packaged as a JAR file, add this line to your build.sbt file:
// set the main class for packaging the main jar mainClass in (Compile, packageBin) := Some("com.alvinalexander.Foo")
That setting tells SBT to add the following line to the META-INF/MANIFEST.MF file in your JAR when you run
When running your application with SBT, you can also use SBT’s
run-main command to specify the class to run. Invoke it like this from your operating system command line:
$ sbt "run-main com.alvinalexander.Foo" [info] Loading global plugins from /Users/Al/.sbt/plugins [info] Running com.alvinalexander.Foo hello [success] Total time: 1 s
Invoke it like this from inside the SBT shell:
$ sbt > run-main com.alvinalexander.Foo [info] Running com.alvinalexander.Foo hello [success] Total time: 1 s
If you have only one
main method in an object in your project (or one object that extends the
App trait), SBT can automatically determine the location of that
main method. In that case, these configuration lines aren’t necessary.
If you have multiple
main methods in your project, and don’t use any of the approaches shown in the Solution, SBT will prompt you with a list of objects it finds that have a
main method or extend the
App trait when you execute
Multiple main classes detected, select one to run:  com.alvinalexander.testproject.Foo  com.alvinalexander.testproject.Bar
The following code shows what a build.sbt file with both of the
mainClass settings looks like:
name := "Simple Test Project" version := "1.0" scalaVersion := "2.10.0" // set the main class for packaging the main jar mainClass in (Compile, packageBin) := Some("com.alvinalexander.testproject.Foo") // set the main class for the main 'sbt run' task mainClass in (Compile, run) := Some("com.alvinalexander.testproject.Foo")
- The SBT Quick Configuration documentation shows dozens of build.sbt examples
The Scala Cookbook
This tutorial is sponsored by the Scala Cookbook, which I wrote for O’Reilly, and which you can find on Amazon.com:
I hope it has been helpful. All the best, Al.
I believe it "runMain" and not "run-main" to execute the class from commandline.
ref- SBT docs: runMain * Runs the specified main class for the project in the same virtual machine as sbt. The main class is passed the arguments provided. Please see Running Project Code for details on the use of System.exit and multithreading (including GUIs) in code run by this action. test:runMain runs the specified main class in the test code.
The command should be runMain now instead of run-main