Reputation: 16287
In a groovy script I start an external process with:
def proc = command.execute()
proc.consumeProcessOutput( System.out, System.err )
proc.waitFor()
println "\nDone. Exit value: ${proc.exitValue()}"
But how do I write the output from the application if it fails?
There is:
proc.errorStream
proc.outputStream
But maybe the output is automatically written when using the consumeProcessOutput
method?
Upvotes: 2
Views: 4705
Reputation: 299
To avoid ‘running-forever’ process (this happens on some Windows env when output exceeds 4096 bytes) add initial size to ByteArrayOutputStream
def initialSize = 4096
def out = new ByteArrayOutputStream(initialSize)
def err = new ByteArrayOutputStream(initialSize)
def proc = command.execute()
proc.consumeProcessOutput(out, err)
proc.waitFor()
Upvotes: 0
Reputation: 66059
If you pass System.out
and System.err
to consumeProcessOutput()
, the output and error streams will both printed out automatically. If you want to capture it, pass in your own output steams:
def out = new ByteArrayOutputStream()
def err = new ByteArrayOutputStream()
def proc = command.execute()
proc.consumeProcessOutput(out, err)
proc.waitFor()
println "error stream was ${err.toString()}"
Upvotes: 4