You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Razvan Surdulescu <ra...@post.harvard.edu> on 2003/05/15 20:45:46 UTC
[Lang] Improved Runtime.exec
I am interested in adding an improved Runtime.exec wrapper implementation to
the Lang commons. By improved I mean that it would handle pumping the
stdout/stderr/stdin streams and a few other minor tweaks.
I looked through the ANT source and noticed that the Exec task has a private
implementation of something like this. Personally speaking, I have written
something like this many times in the past for lack of a standard extant
implementation.
Thoughts, comments?
Razvan.
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [Lang] Improved Runtime.exec
Posted by Alex Chaffee / Purple Technology <gu...@stinky.com>.
On Thu, May 15, 2003 at 01:45:46PM -0500, Razvan Surdulescu wrote:
> I am interested in adding an improved Runtime.exec wrapper implementation to
> the Lang commons. By improved I mean that it would handle pumping the
> stdout/stderr/stdin streams and a few other minor tweaks.
>
> I looked through the ANT source and noticed that the Exec task has a private
> implementation of something like this. Personally speaking, I have written
> something like this many times in the past for lack of a standard extant
> implementation.
Yup! I agree this would be a great addition. You can see one stab
of this in www.purpletech.com/code/src/com/purpletech/util/Utils.java
Or here, I'll just paste it in. Note that all the Alarm thread does
is wait a prescribed time and send an interrupt, to allow a timeout.
I'm still not quite sure this works robustly (since Runtime.exec
itself has poorly defined semantics for these cases).
/**
* Class encapsulating information from an exec call -- slightly
* easier than the standard API
**/
public static class ExecInfo {
public int exit;
public String stdout;
public String stderr;
public String toString() {
return "ExecInfo[exit=" + exit + "," +
"stdout=" + javaEscape(stdout) + "," +
"stderr=" + javaEscape(stderr) + "]";
}
}
/**
* Wrapper for Runtime.exec. Takes input as a String. Times out
* after sleep msec. Returns an object containing exit value,
* standard output, and error output.
* @param command the command-line to execute
* @param input a string to pass to the process as standard input
* @param sleep msec to wait before terminating process (if <= 0, waits forever)
**/
public static ExecInfo exec(String command, String input, long sleep) throws IOException {
Process process = null;
ExecInfo info = new ExecInfo();
try {
Alarm a = null;
if (sleep>0) {
a = new Alarm(Thread.currentThread(), sleep);
a.start();
}
process = Runtime.getRuntime().exec(command);
if (input != null) {
PrintWriter pw = new PrintWriter(process.getOutputStream());
pw.print(input);
pw.close();
}
info.stdout = IOUtils.readStream(process.getInputStream());
info.stderr = IOUtils.readStream(process.getErrorStream());
process.waitFor();
if (a!=null) a.stop = true;
}
catch (InterruptedIOException iioe) {
throw new IOException("Process '" + command + "' took more than " + sleep/1000 + " sec");
}
catch (InterruptedException ie) {
throw new IOException("Process '" + command + "' took more than " + sleep/1000 + " sec");
}
finally {
if (process != null)
process.destroy();
}
info.exit = process.exitValue();
return info;
}
--
Alex Chaffee mailto:alex@jguru.com
Purple Technology - Code and Consulting http://www.purpletech.com/
jGuru - Java News and FAQs http://www.jguru.com/alex/
Gamelan - the Original Java site http://www.gamelan.com/
Stinky - Art and Angst http://www.stinky.com/
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [Lang] Improved Runtime.exec
Posted by Henri Yandell <ba...@generationjava.com>.
If we can support the Ant version, it definitely gets my vote. Refactoring
code out of other Apache Java projects [and accepting donations from them]
is the major part of our reason for being.
Hen
On Thu, 15 May 2003, Razvan Surdulescu wrote:
> I am interested in adding an improved Runtime.exec wrapper implementation to
> the Lang commons. By improved I mean that it would handle pumping the
> stdout/stderr/stdin streams and a few other minor tweaks.
>
> I looked through the ANT source and noticed that the Exec task has a private
> implementation of something like this. Personally speaking, I have written
> something like this many times in the past for lack of a standard extant
> implementation.
>
> Thoughts, comments?
>
> Razvan.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org