Class ProcessExecutor


  • public class ProcessExecutor
    extends java.lang.Object
    Process Executor

    Executes operating system commands and manages the execution lifecycle, including handling input/output streams, monitoring process status, and enforcing timeouts. This class wraps command execution in a structured way to provide enhanced control and integration with the framework.

    Example Usage

         // Execute a simple command with options and capture output
         ProcessExecutor executor = new ProcessExecutor("ls", "-l", "/home");
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         executor.execute(outputStream);
         System.out.println("Command Output: " + outputStream.toString());
    
         // Execute a command with timeout protection
         try {
             executor.execute(outputStream, 5000); // 5 seconds timeout
         } catch (TimeoutException e) {
             System.err.println("Command timed out!");
         }
     
    Since:
    1.0.0
    Author:
    Mercy
    See Also:
    ProcessManager
    • Constructor Summary

      Constructors 
      Constructor Description
      ProcessExecutor​(java.lang.String command, java.lang.String... options)
      Constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void execute​(java.io.OutputStream outputStream)
      Execute current process.
      void execute​(java.io.OutputStream outputStream, long timeoutInMilliseconds)
      Execute current process.
      void execute​(java.io.OutputStream outputStream, long timeout, java.util.concurrent.TimeUnit timeUnit)
      Execute current process.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_PROCESS_EXECUTION_TIMEOUT_PROPERTY_VAUE

        public static final java.lang.String DEFAULT_PROCESS_EXECUTION_TIMEOUT_PROPERTY_VAUE
        The default proeprty value for the timeout of process execution : 30 seconds
        See Also:
        Constant Field Values
      • DEFAULT_PROCESS_EXECUTION_TIMEOUT

        public static final long DEFAULT_PROCESS_EXECUTION_TIMEOUT
        The default value for the timeout of process execution : 30 seconds
      • DEFAULT_TIMEOUT

        public static final long DEFAULT_TIMEOUT
        the timeout of process execution
    • Constructor Detail

      • ProcessExecutor

        public ProcessExecutor​(java.lang.String command,
                               java.lang.String... options)
        Constructor
        Parameters:
        command - command
        options - command options
    • Method Detail

      • execute

        public void execute​(java.io.OutputStream outputStream)
                     throws java.io.IOException,
                            java.util.concurrent.TimeoutException
        Execute current process.
        Parameters:
        outputStream - output stream for process normal or error input stream.
        Throws:
        java.io.IOException - if process execution is failed.
        java.util.concurrent.TimeoutException - if the execution is timeout over specified DEFAULT_TIMEOUT
      • execute

        public void execute​(java.io.OutputStream outputStream,
                            long timeoutInMilliseconds)
                     throws java.io.IOException,
                            java.util.concurrent.TimeoutException
        Execute current process.
        Parameters:
        outputStream - output stream for process normal or error input stream.
        timeoutInMilliseconds - milliseconds timeout
        Throws:
        java.io.IOException - if process execution is failed.
        java.util.concurrent.TimeoutException - if the execution is timeout over specified timeoutInMilliseconds
      • execute

        public void execute​(java.io.OutputStream outputStream,
                            long timeout,
                            java.util.concurrent.TimeUnit timeUnit)
                     throws java.io.IOException,
                            java.util.concurrent.TimeoutException
        Execute current process.
        Parameters:
        outputStream - output stream for process normal or error input stream.
        timeout - the timeout value
        timeUnit - TimeUnit
        Throws:
        java.io.IOException - if process execution is failed.
        java.util.concurrent.TimeoutException - if the execution is timeout over specified timeout and timeUnit