SBT: How to pass command line arguments to ‘sbt run’

Question: How do I pass command-line parameters to my Scala application when I’m running the application with SBT?

Solution: There are two different possible scenarios here:

  • You’re running from inside the SBT shell
  • You’re running SBT from your operating system command line

I’ll show how both of those work.

Back to top

Running inside the SBT shell

When you’re running from inside the SBT shell, just add your parameters after the SBT run command, like this:

sbt> run foo bar baz

That command will work even if you have multiple main methods (or App objects) in your SBT project:

sbt> run foo bar baz
[warn] Multiple main classes detected.
Run 'show discoveredMainClasses' to see the list

Multiple main classes detected, select one to run:

 [1] Test
 [2] Test1

Enter number: 1

[info] Running Test foo bar baz
args.length =  3
foo
bar
baz
Back to top

Running SBT from your operating system command line

When you’re running the SBT run command from your operating system command line, enclose the run command and the command-line parameters in quotes, like this:

$ sbt "run foo bar baz"

This process also works when there are multiple main methods in your project:

$ sbt "run foo bar baz"
[warn] Executing in batch mode.
[warn]   For better performance, hit [ENTER] to switch to interactive mode, or
[warn]   consider launching sbt without any commands, or explicitly passing 'shell'
[info] Loading project definition from /Users/al/...
[warn] Multiple main classes detected.  Run 'show discoveredMainClasses' to see the list

Multiple main classes detected, select one to run:

 [1] Test
 [2] Test1

Enter number: 1

[info] Running Test foo bar baz
args.length =  3
foo
bar
baz
Back to top

More information

If you want to experiment with this on your own, I put a couple of test classes you can use in this Github repository:

There are two Scala files there named Test.scala and Test1.scala that you can start with, especially if you want to test more complicated situations.

Back to top