InheritIO and redirecting stdout












0















I'm trying to write a test for a class that uses ProcessBuilder.inheritIO and I can't seem to get it to work. I've stripped the code down to the core and came up with the following example that I think should write all the output of the child process to a ByteArrayOutputStream but doesn't.



Environment: Java 9.0.4 on Windows 7



import java.io.*;

public class ProcessTest {
public static void main(String args) throws Throwable {
PrintStream original = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
System.setOut(ps);

System.out.println("BEGIN");
Process process = new ProcessBuilder().command("where", "where")
.inheritIO().start();
process.waitFor();

System.out.println("END");
String output = new String(baos.toByteArray());
original.println("output = " + output);
}
}


The output is:



C:WindowsSystem32where.exe
output = BEGIN
END


And when I look in the debugger, the output stream does not contain the path to where.



I'm thinking System.setOut isn't doing what I'm thinking but I'm not sure. I found other SO questions suggest a StreamGrabber but I thought that was for Java 6 and earlier and inheritIO would work for me. Any help would be appreciated. Thanks.



Edit
I've come to realize that calling System.setOut() isn't really doing what I hope. It is not changing the underlying file descriptor of 1. If I was doing this in C, I would be working with the low level file descriptors. But that's not what setOut() is doing. That's just changing pointers.










share|improve this question

























  • I have just verified I'm seeing the same behavior under CentOS, changing "where" to "which".

    – Mike Brazinski
    Nov 21 '18 at 14:46
















0















I'm trying to write a test for a class that uses ProcessBuilder.inheritIO and I can't seem to get it to work. I've stripped the code down to the core and came up with the following example that I think should write all the output of the child process to a ByteArrayOutputStream but doesn't.



Environment: Java 9.0.4 on Windows 7



import java.io.*;

public class ProcessTest {
public static void main(String args) throws Throwable {
PrintStream original = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
System.setOut(ps);

System.out.println("BEGIN");
Process process = new ProcessBuilder().command("where", "where")
.inheritIO().start();
process.waitFor();

System.out.println("END");
String output = new String(baos.toByteArray());
original.println("output = " + output);
}
}


The output is:



C:WindowsSystem32where.exe
output = BEGIN
END


And when I look in the debugger, the output stream does not contain the path to where.



I'm thinking System.setOut isn't doing what I'm thinking but I'm not sure. I found other SO questions suggest a StreamGrabber but I thought that was for Java 6 and earlier and inheritIO would work for me. Any help would be appreciated. Thanks.



Edit
I've come to realize that calling System.setOut() isn't really doing what I hope. It is not changing the underlying file descriptor of 1. If I was doing this in C, I would be working with the low level file descriptors. But that's not what setOut() is doing. That's just changing pointers.










share|improve this question

























  • I have just verified I'm seeing the same behavior under CentOS, changing "where" to "which".

    – Mike Brazinski
    Nov 21 '18 at 14:46














0












0








0








I'm trying to write a test for a class that uses ProcessBuilder.inheritIO and I can't seem to get it to work. I've stripped the code down to the core and came up with the following example that I think should write all the output of the child process to a ByteArrayOutputStream but doesn't.



Environment: Java 9.0.4 on Windows 7



import java.io.*;

public class ProcessTest {
public static void main(String args) throws Throwable {
PrintStream original = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
System.setOut(ps);

System.out.println("BEGIN");
Process process = new ProcessBuilder().command("where", "where")
.inheritIO().start();
process.waitFor();

System.out.println("END");
String output = new String(baos.toByteArray());
original.println("output = " + output);
}
}


The output is:



C:WindowsSystem32where.exe
output = BEGIN
END


And when I look in the debugger, the output stream does not contain the path to where.



I'm thinking System.setOut isn't doing what I'm thinking but I'm not sure. I found other SO questions suggest a StreamGrabber but I thought that was for Java 6 and earlier and inheritIO would work for me. Any help would be appreciated. Thanks.



Edit
I've come to realize that calling System.setOut() isn't really doing what I hope. It is not changing the underlying file descriptor of 1. If I was doing this in C, I would be working with the low level file descriptors. But that's not what setOut() is doing. That's just changing pointers.










share|improve this question
















I'm trying to write a test for a class that uses ProcessBuilder.inheritIO and I can't seem to get it to work. I've stripped the code down to the core and came up with the following example that I think should write all the output of the child process to a ByteArrayOutputStream but doesn't.



Environment: Java 9.0.4 on Windows 7



import java.io.*;

public class ProcessTest {
public static void main(String args) throws Throwable {
PrintStream original = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
System.setOut(ps);

System.out.println("BEGIN");
Process process = new ProcessBuilder().command("where", "where")
.inheritIO().start();
process.waitFor();

System.out.println("END");
String output = new String(baos.toByteArray());
original.println("output = " + output);
}
}


The output is:



C:WindowsSystem32where.exe
output = BEGIN
END


And when I look in the debugger, the output stream does not contain the path to where.



I'm thinking System.setOut isn't doing what I'm thinking but I'm not sure. I found other SO questions suggest a StreamGrabber but I thought that was for Java 6 and earlier and inheritIO would work for me. Any help would be appreciated. Thanks.



Edit
I've come to realize that calling System.setOut() isn't really doing what I hope. It is not changing the underlying file descriptor of 1. If I was doing this in C, I would be working with the low level file descriptors. But that's not what setOut() is doing. That's just changing pointers.







java io processbuilder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 14:12







Mike Brazinski

















asked Nov 21 '18 at 13:48









Mike BrazinskiMike Brazinski

11




11













  • I have just verified I'm seeing the same behavior under CentOS, changing "where" to "which".

    – Mike Brazinski
    Nov 21 '18 at 14:46



















  • I have just verified I'm seeing the same behavior under CentOS, changing "where" to "which".

    – Mike Brazinski
    Nov 21 '18 at 14:46

















I have just verified I'm seeing the same behavior under CentOS, changing "where" to "which".

– Mike Brazinski
Nov 21 '18 at 14:46





I have just verified I'm seeing the same behavior under CentOS, changing "where" to "which".

– Mike Brazinski
Nov 21 '18 at 14:46












1 Answer
1






active

oldest

votes


















0














System.setOut changes the Java process’s standard output. There’s no guarantee it will affect child processes.



Regardless, it’s probably not a good idea to alter a global setting just to capture the output of a single process, especially if you expect your code to run amidst other other code (libraries, appications, etc.).



Solution: Don’t try to hack System.out. Read from the Process’s stdout InputStream:



ProcessBuilder builder = new ProcessBuilder().command("where", "where").inheritIO();
builder.redirectOutput(ProcessBuilder.Redirect.PIPE);
Process process = builder.start();

String output;
try (InputStream processStdOut = process.getInputStream()) {
output = new String(processStdOut.readAllBytes());
}

process.waitFor();





share|improve this answer
























  • I'm trying to avoid opening new streams as the real world version of this starts 16 processes. The legacy code (pre-Java 5) I'm updating uses 64 threads to monitor the processes (1 each for stdin, out and err and a watchdog thread) and merge the pipes. I was hoping inheritIO would take care of a lot of that for me.

    – Mike Brazinski
    Nov 23 '18 at 13:58











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413530%2finheritio-and-redirecting-stdout%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














System.setOut changes the Java process’s standard output. There’s no guarantee it will affect child processes.



Regardless, it’s probably not a good idea to alter a global setting just to capture the output of a single process, especially if you expect your code to run amidst other other code (libraries, appications, etc.).



Solution: Don’t try to hack System.out. Read from the Process’s stdout InputStream:



ProcessBuilder builder = new ProcessBuilder().command("where", "where").inheritIO();
builder.redirectOutput(ProcessBuilder.Redirect.PIPE);
Process process = builder.start();

String output;
try (InputStream processStdOut = process.getInputStream()) {
output = new String(processStdOut.readAllBytes());
}

process.waitFor();





share|improve this answer
























  • I'm trying to avoid opening new streams as the real world version of this starts 16 processes. The legacy code (pre-Java 5) I'm updating uses 64 threads to monitor the processes (1 each for stdin, out and err and a watchdog thread) and merge the pipes. I was hoping inheritIO would take care of a lot of that for me.

    – Mike Brazinski
    Nov 23 '18 at 13:58
















0














System.setOut changes the Java process’s standard output. There’s no guarantee it will affect child processes.



Regardless, it’s probably not a good idea to alter a global setting just to capture the output of a single process, especially if you expect your code to run amidst other other code (libraries, appications, etc.).



Solution: Don’t try to hack System.out. Read from the Process’s stdout InputStream:



ProcessBuilder builder = new ProcessBuilder().command("where", "where").inheritIO();
builder.redirectOutput(ProcessBuilder.Redirect.PIPE);
Process process = builder.start();

String output;
try (InputStream processStdOut = process.getInputStream()) {
output = new String(processStdOut.readAllBytes());
}

process.waitFor();





share|improve this answer
























  • I'm trying to avoid opening new streams as the real world version of this starts 16 processes. The legacy code (pre-Java 5) I'm updating uses 64 threads to monitor the processes (1 each for stdin, out and err and a watchdog thread) and merge the pipes. I was hoping inheritIO would take care of a lot of that for me.

    – Mike Brazinski
    Nov 23 '18 at 13:58














0












0








0







System.setOut changes the Java process’s standard output. There’s no guarantee it will affect child processes.



Regardless, it’s probably not a good idea to alter a global setting just to capture the output of a single process, especially if you expect your code to run amidst other other code (libraries, appications, etc.).



Solution: Don’t try to hack System.out. Read from the Process’s stdout InputStream:



ProcessBuilder builder = new ProcessBuilder().command("where", "where").inheritIO();
builder.redirectOutput(ProcessBuilder.Redirect.PIPE);
Process process = builder.start();

String output;
try (InputStream processStdOut = process.getInputStream()) {
output = new String(processStdOut.readAllBytes());
}

process.waitFor();





share|improve this answer













System.setOut changes the Java process’s standard output. There’s no guarantee it will affect child processes.



Regardless, it’s probably not a good idea to alter a global setting just to capture the output of a single process, especially if you expect your code to run amidst other other code (libraries, appications, etc.).



Solution: Don’t try to hack System.out. Read from the Process’s stdout InputStream:



ProcessBuilder builder = new ProcessBuilder().command("where", "where").inheritIO();
builder.redirectOutput(ProcessBuilder.Redirect.PIPE);
Process process = builder.start();

String output;
try (InputStream processStdOut = process.getInputStream()) {
output = new String(processStdOut.readAllBytes());
}

process.waitFor();






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 15:41









VGRVGR

22.8k42840




22.8k42840













  • I'm trying to avoid opening new streams as the real world version of this starts 16 processes. The legacy code (pre-Java 5) I'm updating uses 64 threads to monitor the processes (1 each for stdin, out and err and a watchdog thread) and merge the pipes. I was hoping inheritIO would take care of a lot of that for me.

    – Mike Brazinski
    Nov 23 '18 at 13:58



















  • I'm trying to avoid opening new streams as the real world version of this starts 16 processes. The legacy code (pre-Java 5) I'm updating uses 64 threads to monitor the processes (1 each for stdin, out and err and a watchdog thread) and merge the pipes. I was hoping inheritIO would take care of a lot of that for me.

    – Mike Brazinski
    Nov 23 '18 at 13:58

















I'm trying to avoid opening new streams as the real world version of this starts 16 processes. The legacy code (pre-Java 5) I'm updating uses 64 threads to monitor the processes (1 each for stdin, out and err and a watchdog thread) and merge the pipes. I was hoping inheritIO would take care of a lot of that for me.

– Mike Brazinski
Nov 23 '18 at 13:58





I'm trying to avoid opening new streams as the real world version of this starts 16 processes. The legacy code (pre-Java 5) I'm updating uses 64 threads to monitor the processes (1 each for stdin, out and err and a watchdog thread) and merge the pipes. I was hoping inheritIO would take care of a lot of that for me.

– Mike Brazinski
Nov 23 '18 at 13:58




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413530%2finheritio-and-redirecting-stdout%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter