Package util

Class CmdExecutor

All Implemented Interfaces:
Serializable, Runnable

public class CmdExecutor extends Executor
Class for executing an external program as a process.

If the process cannot be created, is aborted, is externally interrupted, or terminates normally or with failure, a listener function is notified, with an identifier of the event that occurred. The exit value of the process can be retrieved with getResult() as an Integer.

The output of the process (stdout and stderr) is copied and also stored in internal buffers, which can be accessed to check the process result.

Since:
2007.02.10
See Also:
  • Constructor Details

    • CmdExecutor

      public CmdExecutor(String[] command, ActionListener listener)
      Create a command executor.

      On termination, the listener is notified. The identifier of the action event that is passed to the listener is OK if the process terminated successfully, ABORTED if the process was aborted by a call to the abort() function, INTERRUPTED if the process was externally interrupted, and FAILED if the process could not be started it returned a non-zero exit code.

      Parameters:
      command - the command to be executed
      listener - the listener to be notified on termination
      Since:
      2007.02.10 (Christian Borgelt)
  • Method Details

    • run

      public void run()
      Function for thread execution.
      Specified by:
      run in interface Runnable
      Specified by:
      run in class Executor
      Since:
      2007.02.10 (Christian Borgelt)
    • getCommand

      public String[] getCommand()
      Get the command to execute as a process.
      Returns:
      the command to execute as a process
      Since:
      2007.02.10 (Christian Borgelt)
    • exitValue

      public int exitValue()
      Get the exit value of the executed command.

      The exit value of the command is available only after the process terminated, that is, after the status of the executor has changed to either OK or FAILED. Note that in case the command could not be started (and thus could not produce an exit value), this function returns 0.

      Returns:
      the exit value of the executed command
      Since:
      2007.05.03 (Christian Borgelt)
    • getOutputData

      public String getOutputData()
      Get the output stream data of the process.

      This function may be called any time and yields the output of the process (to the standard output stream) that has been generated up to that point. If the process has not been started yet, the result is null.

      Returns:
      the output stream data of the process as a string
      Since:
      2007.02.10 (Christian Borgelt)
    • getLastOutputLine

      public String getLastOutputLine()
      Get the last line of the output stream data of the process.
      Returns:
      the last line of the output stream data of the process
      Since:
      2007.02.10 (Christian Borgelt)
      See Also:
    • getErrorData

      public String getErrorData()
      Get the error stream data of the process.

      This function may be called any time and yields the output of the process (to the standard error stream) that has been generated up to that point. If the process has not been started yet, the result is null.

      Returns:
      the error stream data of the process as a string
      Since:
      2007.02.10 (Christian Borgelt)
    • getLastErrorLine

      public String getLastErrorLine()
      Get the last line of the error stream data of the process.
      Returns:
      the last line of the error stream data of the process
      Since:
      2007.02.10 (Christian Borgelt)
      See Also: