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: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Getting results of command execution Date: Wed, 17 Jun 2015 17:14:35 -0400 Organization: A noiseless patient Spider Lines: 26 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 17 Jun 2015 21:13:18 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="858a9d65d95f4ea4d355bd116aa2cdbf"; logging-data="14829"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19sIm86PDIzZa2PJHIvNm5L" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: Cancel-Lock: sha1:QbGeVV/tF2Mw7yTHu3dLjQVhaaA= Xref: csiph.com comp.lang.java.programmer:31815 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. -- esosman@comcast-dot-net.invalid "Don't be afraid of work. Make work afraid of you." -- TLM