SBT: Start a command line 'run' of the main class of a non-default project

ScalaSbt

Scala Problem Overview


I am starting to use sbt build my Scala code (and handle dependencies). As far as I know if I use

$ sbt run <args>

on the command line this will run the main class of the main project.

Is it possible to 'run' within any other project from the command line, i.e. not in the interactive session mode? (I'm thinking about something that might look like $ sbt project <proj> run <args> or whatever...)

What I would do in interactive mode is this:

$ sbt
> project <projectname>
> run <args>

This seems to be straightforward enough, but I can not find any documentation describing this behavior. Hints would be much appreciated...

Scala Solutions


Solution 1 - Scala

You simply have to quote each command (as in the second example on this page), so in your case it would be:

$ sbt "project foo" "run arg1 arg2"

Solution 2 - Scala

$ sbt foo/run arg1 arg2 also work

Solution 3 - Scala

This works: sbt "runMain com.example.Hello arg1" or sbt "run-main com.example.Hello arg1".

See here for reference: https://blog.ssanj.net/posts/2016-03-02-how-to-run-a-specific-main-class-with-parameters-through-sbt.html

Solution 4 - Scala

Worked for me:

$ sbt "run someNumber"

Also this may be of some help:

def main(args: Array[String]) {
	val n = args(0).toInt
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfgysinView Question on Stackoverflow
Solution 1 - ScalaMoritzView Answer on Stackoverflow
Solution 2 - ScalaGuillaume MasséView Answer on Stackoverflow
Solution 3 - ScalaXåpplI'-I0llwlg'I -View Answer on Stackoverflow
Solution 4 - ScalarandompastView Answer on Stackoverflow