commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alex Chaffee / Purple Technology <g...@stinky.com>
Subject Re: [Lang] Improved Runtime.exec
Date Sat, 24 May 2003 02:27:56 GMT

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


Mime
View raw message