문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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()
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top