Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redirectError example is confusing #100

Open
micobarac opened this issue Dec 30, 2021 · 3 comments
Open

redirectError example is confusing #100

micobarac opened this issue Dec 30, 2021 · 3 comments

Comments

@micobarac
Copy link

micobarac commented Dec 30, 2021

In the following example, the console gets all the output, but the String output value remains empty:

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

Output:

2021-12-30 12:51:32.150 DEBUG 43928 --- [  XNIO-1 task-1] org.zeroturnaround.exec.ProcessExecutor  : Executing [java, -version].
2021-12-30 12:51:32.177 DEBUG 43928 --- [  XNIO-1 task-1] org.zeroturnaround.exec.ProcessExecutor  : Started java.lang.UNIXProcess@56e2d396
2021-12-30 12:51:32.296  INFO 43928 --- [      Thread-56] c.l.openmx.service.mapper.AccountMapper  : openjdk version "1.8.0_292"
2021-12-30 12:51:32.297  INFO 43928 --- [      Thread-56] c.l.openmx.service.mapper.AccountMapper  : OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
2021-12-30 12:51:32.297  INFO 43928 --- [      Thread-56] c.l.openmx.service.mapper.AccountMapper  : OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)
2021-12-30 12:51:32.300 DEBUG 43928 --- [  XNIO-1 task-1] org.zeroturnaround.exec.WaitForProcess   : java.lang.UNIXProcess@56e2d396 stopped with exit code 0

Without redirectError, the String output gets the output value:

String output = new ProcessExecutor().command("java", "-version")
        .readOutput(true).execute()
        .outputUTF8();

Output:

openjdk version "1.8.0_292"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.292-b10, mixed mode)

Questions:

  1. Does redirectError work just in case of errors?
  2. Should String output get the output value with redirectError in place?
@nemecec
Copy link
Collaborator

nemecec commented Feb 22, 2023

You see a combination of two aspects:

  1. java -version command prints the version information to standard error stream.
  2. Unless an explicit handler for standard error stream has been specified, the ProcessExecutor by default redirects standard error stream into the standard output stream and returns the merged result as "output".

@panchenko
Copy link
Contributor

This example is taken from the README file, would be good replacing it with something which makes more sense.

@nemecec
Copy link
Collaborator

nemecec commented Mar 15, 2023

Well, the README does describe the behaviour:

  • Pumping the stderr to a logger
  • Returning the output as UTF8 String

Maybe should replace the second line with "Returning the output as UTF-8 String (empty in this case as the version is printed to stderr)". Would this be enough?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants