Question

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?

Was it helpful?

Solution

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()}"

OTHER TIPS

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top