Class CommandExec

java.lang.Object
com.pnfsoftware.jeb.util.concurrent.CommandExec

public class CommandExec extends Object
A wrapper around Process to execute short commands, in a blocking or non-blocking way with or without a timeout, and retrieve the exit code and generated output(s).

A lower-level alternative that offers access to the streams (including stdin) is ProcessWrapper.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create an object that will execute commands in blocking mode, without timeout.
    CommandExec(long timeoutMs, boolean redirectErrToOut)
    Create an object that will execute commands.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    execute(String... cmdarray)
    Execute a command.
    boolean
    Execute a command.
    byte[]
    Retrieve the command execution's error bytes, if execution completed.
    Retrieve the command execution's exit value, if execution completed.
    byte[]
    Retrieve the command execution's output bytes, if execution completed.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CommandExec

      public CommandExec()
      Create an object that will execute commands in blocking mode, without timeout.
    • CommandExec

      public CommandExec(long timeoutMs, boolean redirectErrToOut)
      Create an object that will execute commands.
      Parameters:
      timeoutMs - timeout for the command execution, in milliseconds:
      • zero: blocking, without timeout
      • positive value: blocking, and target will be killed when timeout has elapsed
      • negative value: non-blocking, execute returns true immediately; subsequent calls to getOutput() return null
      redirectErrToOut - if true, the error stream will be redirected to the output stream; this flag is disregarded if the timeout is negative
  • Method Details

    • getExitValue

      public Integer getExitValue()
      Retrieve the command execution's exit value, if execution completed. Never available in non-blocking mode.
      Returns:
      the exit value or null
    • getOutput

      public byte[] getOutput()
      Retrieve the command execution's output bytes, if execution completed. Never available in non-blocking mode.
      Returns:
      the bytes or null
    • getError

      public byte[] getError()
      Retrieve the command execution's error bytes, if execution completed. Never available in non-blocking mode.
      Returns:
      the bytes or null
    • execute

      public boolean execute(Collection<String> cmdlist)
      Execute a command. The method may block, depending on the timeout value provided to the constructor.
      Parameters:
      cmdlist - the command tokens (command and arguments), eg: ["ls", "-l", "*.java"]
      Returns:
      true in non-blocking mode or if execution completed, false otherwise
    • execute

      public boolean execute(String... cmdarray)
      Execute a command. The method may block, depending on the timeout value provided to the constructor.
      Parameters:
      cmdarray - the command tokens (command and arguments), eg: ["ls", "-l", "*.java"]
      Returns:
      true in non-blocking mode or if execution completed, false otherwise