Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #31822
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Getting results of command execution |
| Date | 2015-06-18 08:38 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <mlue17$v8u$1@dont-email.me> (permalink) |
| References | <c3df45f4-0172-4986-95e4-cbfb6222610f@googlegroups.com> <mlsnte$efd$1@dont-email.me> <7a28b01f-2a86-4703-98e7-79a7be418abf@googlegroups.com> |
On 6/17/2015 5:57 PM, FredK wrote:
> On Wednesday, June 17, 2015 at 2:14:45 PM UTC-7, Eric Sosman wrote:
>> On 6/17/2015 4:56 PM, FredK wrote:
>>> I want to execute a set of commands and get the results. So I start with,
>>> on Unix;
>>> process P = Runtime.getRuntime().exec("commandA; commandB");
>>>
>>> or on Windows
>>> process P = Runtime.getRuntime().exec("commandA & commandB");
>>>
>>> How do I get the results of the output AND error output in the order they are created?
>>>
>>> If I use this type of coding:
>>> [...]
>>> I get all of the stdout, followed by all of the stderr. But suppose commandA writes some output, then some errors, then more outputs, etc. I want to retrieve them in that order.
>>
>> You could try using a ProcessBuilder to set up your Process, and
>> use its redirectErrorStream() method to merge stdout and stderr before
>> calling start(). But even then, you'll be at the mercy of whatever
>> buffering takes place in the external process itself: If that process
>> holds on to stdout data until it's got a bufferload, but emits stderr
>> data right away (this is the default behavior for many C programs),
>> the mingled data will be temporally scrambled no matter what you do
>> on the receiving side.
>>
>>
> I'm not sure I can accomplish what I want; I haven't been able to find anything that will let me execute multiple commands in the same invocation.
>
> I want to pass along user commands to be executed, and there might be a "cd" imbedded in the string, or setting some environment variables, or any of a multitude of things that cause one command to depend on the results of some previous command(s), so it I can't just execute each command in a separate process.
On Unixoid systems, you do this by executing a shell as the child
process and feeding it the stream of commands you want executed. I'm
not sure what the Windows idiom is -- maybe execute cmd.exe? -- but it's
likely to be something similar.
But that's a separate matter from getting "correct" interleaving
of the stdout and stderr streams, which may be buffered independently
(and differently!) in the various sub-commands. If the commands are
A,B,C I think you can ensure that all of A's output will precede B's,
and all of B's will precede C's, but unless you've got special knowledge
of A,B,C I don't think you can impose any particular ordering of the
stdout and stderr data they generate individually.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Getting results of command execution FredK <fred.l.kleinschmidt@gmail.com> - 2015-06-17 13:56 -0700
Re: Getting results of command execution Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-17 17:14 -0400
Re: Getting results of command execution FredK <fred.l.kleinschmidt@gmail.com> - 2015-06-17 14:57 -0700
Re: Getting results of command execution Martin Gregorie <martin@address-in-sig.invalid> - 2015-06-17 23:07 +0000
Re: Getting results of command execution Jeff Higgins <jeff@invalid.invalid> - 2015-06-17 21:26 -0400
Re: Getting results of command execution FredK <fred.l.kleinschmidt@gmail.com> - 2015-06-18 07:18 -0700
Re: Getting results of command execution FredK <fred.l.kleinschmidt@gmail.com> - 2015-06-18 07:30 -0700
Re: Getting results of command execution Jeff Higgins <jeff@invalid.invalid> - 2015-06-18 17:37 -0400
Re: Getting results of command execution FredK <fred.l.kleinschmidt@gmail.com> - 2015-06-19 07:50 -0700
Re: Getting results of command execution Jeff Higgins <jeff@invalid.invalid> - 2015-06-19 21:33 -0400
Re: Getting results of command execution Jeff Higgins <jeff@invalid.invalid> - 2015-06-19 21:56 -0400
Re: Getting results of command execution Jeff Higgins <jeff@invalid.invalid> - 2015-06-19 23:02 -0400
Re: Getting results of command execution Jeff Higgins <jeff@invalid.invalid> - 2015-06-20 11:11 -0400
Re: Getting results of command execution Jeff Higgins <jeff@invalid.invalid> - 2015-06-20 12:14 -0400
Re: Getting results of command execution Jeff Higgins <jeff@invalid.invalid> - 2015-06-20 20:58 -0400
Re: Getting results of command execution Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-19 11:01 -0400
Re: Getting results of command execution Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-18 08:38 -0400
Re: Getting results of command execution Jeff Higgins <jeff@invalid.invalid> - 2015-07-03 23:41 -0400
csiph-web