Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Jeff Higgins Newsgroups: comp.lang.java.programmer Subject: Re: Getting results of command execution Date: Wed, 17 Jun 2015 21:26:23 -0400 Organization: A noiseless patient Spider Lines: 40 Message-ID: References: <7a28b01f-2a86-4703-98e7-79a7be418abf@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 18 Jun 2015 01:27:23 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="8e164d158183579609bf09837dee5210"; logging-data="3468"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jfz6ezWVLhKXVJw0pdVG4ijHoPlVwGSk=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0 In-Reply-To: <7a28b01f-2a86-4703-98e7-79a7be418abf@googlegroups.com> Cancel-Lock: sha1:mwyCtkTfQMFkdMLZfklaKfCoTsY= Xref: csiph.com comp.lang.java.programmer:31818 On 06/17/2015 05: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'm sure that you're clear on what you want to accomplish but so far you haven't made it clear to me. It sounds as if you want to plant a Java process between a user and his CLI (sh, cmd.exe, ...) or perhaps replace it? > 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. >