Class DefaultExecutor

  • All Implemented Interfaces:
    Executor
    Direct Known Subclasses:
    DaemonExecutor

    public class DefaultExecutor
    extends java.lang.Object
    implements Executor
    The default class to start a subprocess. The implementation allows to
    • set a current working directory for the subprocess
    • provide a set of environment variables passed to the subprocess
    • capture the subprocess output of stdout and stderr using an ExecuteStreamHandler
    • kill long-running processes using an ExecuteWatchdog
    • define a set of expected exit values
    • terminate any started processes when the main process is terminating using a ProcessDestroyer
    The following example shows the basic usage:
     Executor exec = new DefaultExecutor();
     CommandLine cl = new CommandLine("ls -l");
     int exitvalue = exec.execute(cl);
     
    Version:
    $Id: DefaultExecutor.java 1636056 2014-11-01 21:12:52Z ggregory $
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultExecutor()
      Default constructor creating a default PumpStreamHandler and sets the working directory of the subprocess to the current working directory.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.Thread createThread​(java.lang.Runnable runnable, java.lang.String name)
      Factory method to create a thread waiting for the result of an asynchronous execution.
      int execute​(CommandLine command)
      Methods for starting synchronous execution.
      int execute​(CommandLine command, java.util.Map<java.lang.String,​java.lang.String> environment)
      Methods for starting synchronous execution.
      void execute​(CommandLine command, java.util.Map<java.lang.String,​java.lang.String> environment, ExecuteResultHandler handler)
      Methods for starting asynchronous execution.
      void execute​(CommandLine command, ExecuteResultHandler handler)
      Methods for starting asynchronous execution.
      protected java.lang.Thread getExecutorThread()
      Get the worker thread being used for asynchronous execution.
      ProcessDestroyer getProcessDestroyer()
      Set the handler for cleanup of started processes if the main process is going to terminate.
      ExecuteStreamHandler getStreamHandler()
      Get the StreamHandler used for providing input and retrieving the output.
      ExecuteWatchdog getWatchdog()
      Get the watchdog used to kill of processes running, typically, too long time.
      java.io.File getWorkingDirectory()
      Get the working directory of the created process.
      boolean isFailure​(int exitValue)
      Checks whether exitValue signals a failure.
      protected java.lang.Process launch​(CommandLine command, java.util.Map<java.lang.String,​java.lang.String> env, java.io.File dir)
      Creates a process that runs a command.
      void setExitValue​(int value)
      Define the exitValue of the process to be considered successful.
      void setExitValues​(int[] values)
      Define a list of exitValue of the process to be considered successful.
      void setProcessDestroyer​(ProcessDestroyer processDestroyer)
      Get the handler for cleanup of started processes if the main process is going to terminate.
      void setStreamHandler​(ExecuteStreamHandler streamHandler)
      Set a custom the StreamHandler used for providing input and retrieving the output.
      void setWatchdog​(ExecuteWatchdog watchDog)
      Set the watchdog used to kill of processes running, typically, too long time.
      void setWorkingDirectory​(java.io.File dir)
      Set the working directory of the created process.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait