3

I want to spawn a process (SBT for that matter) from Scala, along with two SBT-commands passed as arguments. I made an sbt.sh shell script (simplified version of Manual Installation in sbt Documentation):

java -jar /home/bob/sbt/sbt/bin/sbt-launch.jar "$@"

If works fine when I invoke the script from a bash prompt in Ubuntu (/home/bob/workingdir being the working directory):

~/sbt.sh "project Foo" "run"

However, I'd like to invoke it in a Scala program with the following piece of code:

val pseq = Seq("/home/bob/sbt.sh", "\"project Foo\"", "\"run\"")
val pb = scala.sys.process.Process(pseq, new java.io.File("/home/bob/workingdir"))
pb.!

SBT starts and loads normally, then fails with the following error output:

[error] Expected key
[error] "project Foo"
[error] ^

It works fine on Windows 8, but fails on Ubuntu 12.10. My pseq used to be an ordinary String with the full bash command. I turned into a Seq in order to get the argument splitting explicit (rather than at any space, even within quotes).

Can anybody point out what's the difference between the two invocations, or what else I'm doing wrong?

1 Answer 1

5

I came across the solution. Turned out to be trivial, but worth sharing anyway:

// no \"-quoting of passed SBT commands here!
val pseq = Seq("/home/bob/sbt.sh", "project Foo", "run")
val pb = scala.sys.process.Process(pseq, new java.io.File("/home/bob/workingdir"))
pb.!

Neat, and works on both Windows and Linux. Using /home/bob/sbt.sh "project Foo" "run" as a command string is rather unelegant on Windows and does not work on Linux.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.