setOut() and redirectOutput() together
Peter Levart
peter.levart at gmail.com
Thu May 28 13:43:30 UTC 2015
That's right.
System.setOut(OutputStream) only assigns some OutputStream object to the
System.out field, so any Java code using System.out to retrieve the
OutputStream will use the newly set OuputStream.
ProcessBuilder.Redirect.INHERIT on the other hand, tells ProcessBuilder
where to redirect the standard output of the subprocess when it is
started. This has nothing to do with System.out field except that by
default the field holds an OutputStream object that outputs to the
standard output of the java process and that is also where
ProcessBuilder.Redirect.INHERIT instructs ProcessBuilder to redirect the
output of the started subprocess (inherits the output of the java
process). In other words: the "target" of the default System.out and
subprocess redirected output when using ProcessBuilder.Redirect.INHERIT
is the same. When you change System.out, you change it's target, but
that doesn't change where ProcessBuilder.Redirect.INHERIT redirects to.
Regards, Peter
On 05/28/2015 12:48 PM, Dawid Weiss wrote:
> Your code makes the native app inherit native stdout/err process
> descriptors, you need to pipe the output from your native app to Java
> and then redirect the output to wherever you wish (byte array for
> example).
>
> In other words, Redirect.PIPE + pipe code.
>
> Dawid
>
> On Thu, May 28, 2015 at 12:43 PM, Weijun Wang <weijun.wang at oracle.com> wrote:
>> I am writing something like this
>>
>> ByteArrayOutputStream bout = new ByteArrayOutputStream();
>> System.setOut(out);
>>
>> System.out.println("Hello");
>>
>> ProcessBuilder pb1 = new ProcessBuilder("./native-app");
>> pb1.redirectOutput(ProcessBuilder.Redirect.INHERIT);
>>
>> pb1.start();
>>
>> Now "Hello" no longer shows on screen, but the output of "native-app" still
>> shows up there. What else shall I do?
>>
>> This is Ubuntu 14.04.2.
>>
>> Thanks
>> Max
More information about the core-libs-dev
mailing list