I'm working with the Scala scala.sys.process
library.
I know that I can capture the exit code with !
and the output with !!
but what if I want to capture both?
I've seen this answer https://stackoverflow.com/a/6013932/416338 which looks promising, but I'm wondering if there is a one liner and I'm missing something.
I have the following utility method for running commands:
import sys.process._ def runCommand(cmd: Seq[String]): (Int, String, String) = { val stdoutStream = new ByteArrayOutputStream val stderrStream = new ByteArrayOutputStream val stdoutWriter = new PrintWriter(stdoutStream) val stderrWriter = new PrintWriter(stderrStream) val exitValue = cmd.!(ProcessLogger(stdoutWriter.println, stderrWriter.println)) stdoutWriter.close() stderrWriter.close() (exitValue, stdoutStream.toString, stderrStream.toString) }
As you can see, it captures stdout, stderr and result code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With