Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'sbt run' with CLI arguments from shell

Tags:

scala

sbt

I tried running my scala project with CLI arguments using sbt launcher version 0.12.1. It works OK when run from the scala prompt:

$ sbt
[info] (...)
> run sth
(...)
[success] Total time: 0 s, completed Nov 9, 2012 3:04:47 PM

But when I want to run it whole from shell, as per this answer for example, I'm getting an error:

$ sbt "run sth"
[info] (...)
you need to provide source file name
[success] Total time: 0 s, completed Nov 9, 2012 3:07:07 PM
[error] Not a valid command: sth (similar: set, last, shell)
[error] Expected '/'
[error] Expected ':'
[error] Not a valid key: sth (similar: test, state, watch)
[error] sth
[error]    ^

And the "you need to provide source file name" info is given by my scala project to indicate that main didn't get any CLI arguments.

Is it something that worked in the previous version of sbt (in the referenced question), or am I doing something wrong?

like image 218
nietaki Avatar asked Nov 09 '12 14:11

nietaki


People also ask

How do I pass a command line argument in Scala?

The arguments which are passed by the user or programmer to the main() method are termed as Command-Line Arguments. main() method is the entry point of execution of a program. main() method accepts an array of strings.

What is sbt shell?

An sbt shell is embedded in the sbt project and is available on your project start. You can use the sbt shell for executing sbt commands and tasks, for running, and debugging your projects.


2 Answers

The problem could be the way you pass arguments in your sbt launch file to Java. In my case it is:

java -Xmx4096M -jar `dirname $0`/sbt-launch_0.12.1.jar "$@"

And I just confirmed: It works with both 0.12 and 0.12.1. For instance, for a program that just prints its arguments I get:

$ sbt "run-main SomeMain blah blah"
Outut:
[blah, blah]

By using run-main you can also avoid the possibility that sbt somehow does not see your main function.

like image 112
bluenote10 Avatar answered Oct 01 '22 15:10

bluenote10


That's a bug in the 0.12.1 launcher: https://github.com/sbt/sbt-launcher-package/issues/34

like image 41
Lukas Rytz Avatar answered Oct 01 '22 14:10

Lukas Rytz