se.unlogic.standardutils.exec
Class ExecUtils

java.lang.Object
  extended by se.unlogic.standardutils.exec.ExecUtils

public class ExecUtils
extends Object

Utility class for executing processes and handling the output from them.

Author:
Robert "Unlogic" Olofsson (unlogic@unlogic.se)

Constructor Summary
ExecUtils()
           
 
Method Summary
static int execAndWait(ProcessBuilder builder, OutputStream outputStream, Long timeout)
          Executes the given command and waits for the resulting processes to terminate and all data written to standard out and error out to be handled.
static int execAndWait(String command, Long timeout)
          Executes the given command and waits for the resulting processes to terminate and all data written to standard out and error out to be handled.
static int execAndWait(String command, OutputStream outputStream, Long timeout)
          Executes the given command and waits for the resulting processes to terminate and all data written to standard out and error out to be handled.
static int execAndWait(String command, StreamHandler stdOutHandler, StreamHandler errorOutHandler, Long timeout)
          Executes the given command and waits for the resulting processes to terminate and all data written to standard out and error out to be handled.
static int waitForProcessAndStreams(Process proc, StreamHandler stdOutHandler, StreamHandler errorOutHandler, Long timeout)
          Waits for the given processes to terminate and all data written to standard out and error out to be handled.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExecUtils

public ExecUtils()
Method Detail

execAndWait

public static int execAndWait(String command,
                              Long timeout)
                       throws IOException,
                              InterruptedException
Executes the given command and waits for the resulting processes to terminate and all data written to standard out and error out to be handled.

All data written to standard out is piped to System.out with the given prefix

All data written to error out is piped to System.err with the given prefix

Parameters:
command - the command to executed
timeout - an optional timeout that controls how long the process i allowed to run before it is killed
Returns:
the exit value of the process. By convention, 0 indicates normal termination.
Throws:
IOException
InterruptedException

execAndWait

public static int execAndWait(String command,
                              StreamHandler stdOutHandler,
                              StreamHandler errorOutHandler,
                              Long timeout)
                       throws IOException,
                              InterruptedException
Executes the given command and waits for the resulting processes to terminate and all data written to standard out and error out to be handled.

Parameters:
command - the command to executed
stdOutHandler - the StreamHandler to handle all output the process writes on standard out
errorOutHandler - the StreamHandler to handle all output the process writes on error out
timeout - an optional timeout that controls how long the process i allowed to run before it is killed
Returns:
the exit value of the process. By convention, 0 indicates normal termination.
Throws:
IOException
InterruptedException

execAndWait

public static int execAndWait(String command,
                              OutputStream outputStream,
                              Long timeout)
                       throws IOException,
                              InterruptedException
Executes the given command and waits for the resulting processes to terminate and all data written to standard out and error out to be handled.

All data written to standard out and error out is piped to the given OutputStream

Parameters:
command - the command to executed
outputStream - the OutputStream to handle all output of the process
timeout - an optional timeout that controls how long the process i allowed to run before it is killed
Returns:
the exit value of the process. By convention, 0 indicates normal termination.
Throws:
IOException
InterruptedException

execAndWait

public static int execAndWait(ProcessBuilder builder,
                              OutputStream outputStream,
                              Long timeout)
                       throws IOException,
                              InterruptedException
Executes the given command and waits for the resulting processes to terminate and all data written to standard out and error out to be handled.

All data written to standard out and error out is piped to the given OutputStream

Parameters:
builder - the ProcessBuilder to get to start the Process through
outputStream - the OutputStream to handle all output of the process
timeout - an optional timeout that controls how long the process i allowed to run before it is killed
Returns:
the exit value of the process. By convention, 0 indicates normal termination.
Throws:
IOException
InterruptedException

waitForProcessAndStreams

public static int waitForProcessAndStreams(Process proc,
                                           StreamHandler stdOutHandler,
                                           StreamHandler errorOutHandler,
                                           Long timeout)
                                    throws InterruptedException
Waits for the given processes to terminate and all data written to standard out and error out to be handled.

All data written to standard out and error out is piped to the given OutputStream

Parameters:
command - the command to executed
outputStream - the OutputStream to handle all output of the process
Returns:
the exit value of the process. By convention, 0 indicates normal termination.
Throws:
IOException
InterruptedException


Copyright © 2011. All Rights Reserved.