zeroturnaround / zt-exec

ZeroTurnaround Process Executor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: How I can pickup stderr from ProcessResult

dinuta opened this issue · comments

  ProcessResult processResult = processState.getProcessResult().get(timeout, TimeUnit.SECONDS);
  int code = processResult.getExitValue();
  String err = (code == 0) ? "" : processResult.getOutput().getString();

I want to pick up from a Process class stderr using your lib.

This piece of code is not correct since there are commands where exit status is 0 but it logs the result in stderr. E.g. java -version

How can I pickup stderr reliably?

Thanks guys,
Catalin

There is a sample for redirecting stderr to a separate stream on https://github.com/zeroturnaround/zt-exec/:

String output = new ProcessExecutor().command("java", "-version")
        .redirectError(Slf4jStream.of(getClass()).asInfo())
        .readOutput(true).execute()
        .outputUTF8();

Feel free to replace Slf4jStream.of(getClass()).asInfo() with some other OutputStream instance.

Thanks @nemecec . Will give it a try!

Hi @nemecec ,

Do you think is fine? Tested and looked fine. Please have a look:
https://github.com/dinuta/estuary-agent-java/pull/23/files

Are you sure about your test?
As much as I know about piped streams in Java, you are not using them correctly. I would be surprised if this code actually works.
See also http://tutorials.jenkov.com/java-io/pipedinputstream.html

I added some comments on the pull request.

Thanks man for your time.

Did some changes here:
Yes the streams were not used properly, but my integration tests passed ... The service seemed to put stdout and stderr properly in the HTTP response.

https://github.com/dinuta/estuary-agent-java/pull/24/files

I will also add a SLF4j logger.

I will close this with many thanks.
Catalin