Package bm.util

Class Exec

java.lang.Object
bm.util.Exec

public final class Exec extends Object
Somewhat simplified apis around ProcessBuilder to run sub-processes.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    Provides standard and error output and the exit code of a process as strings after the process exits.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Path
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Executes the process and waits for the termination.
    static int
    exec(ProcessBuilder pb, Consumer<Process> afterStart)
    Starts the process defined by the process builder, then applies the given consumer and then waits for the process to teminate.
    static void
    exec(String command, String... args)
    Calls exec(List) after wrapping the arguments into a list.
    static void
    exec(List<String> cmdline)
    Simplified API to start a process with default std(in|out|err) and wait for the outcome.
    static Exec.Out
    exec(List<String> cmdline, Path out, Path err)
    Executes the command line and waits for the termination.
    static Exec.Out
    Executes the command line and reads both, standard output and standard error to return it as part of the result.
    static Exec.Out
    Executes the command line and reads standard error to return it as part of the result.
    static Exec.Out
    Executes the command line and reads standard output to return it as part of the result.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • PATH_EMPTY

      public static final Path PATH_EMPTY
  • Constructor Details

    • Exec

      public Exec()
  • Method Details

    • exec

      public static void exec(String command, String... args) throws BuildProblemException
      Calls exec(List) after wrapping the arguments into a list. Intended for the simple cases with constant string arguments.
      Parameters:
      command - to run
      args - command line parameters for the command
      Throws:
      BuildProblemException
    • exec

      public static void exec(List<String> cmdline) throws BuildProblemException
      Simplified API to start a process with default std(in|out|err) and wait for the outcome.
      Parameters:
      cmdline - to execute. Consider using StringLister to build up the argument list.
      Throws:
      BuildProblemException - if the process return status is not 0
    • execReadBoth

      public static Exec.Out execReadBoth(List<String> cmdline) throws BuildProblemException
      Executes the command line and reads both, standard output and standard error to return it as part of the result.
      Throws:
      BuildProblemException
    • execReadStdout

      public static Exec.Out execReadStdout(List<String> cmdline) throws BuildProblemException
      Executes the command line and reads standard output to return it as part of the result.
      Throws:
      BuildProblemException
    • execReadStderr

      public static Exec.Out execReadStderr(List<String> cmdline) throws BuildProblemException
      Executes the command line and reads standard error to return it as part of the result.
      Throws:
      BuildProblemException
    • exec

      public static Exec.Out exec(List<String> cmdline, Path out, Path err) throws BuildProblemException
      Executes the command line and waits for the termination. Depending on the parameters, standard output and error are either inherited, written to a file or captured and returned in the result.
      Parameters:
      cmdline - to execute
      out - if null, inherit stdout, if a valid Path, send output there, if an empty Path, return it as {#link Exec.Out.stdout()}
      err - like out but for standard error
      Returns:
      the standard and/or error output of the process and its exit code
      Throws:
      BuildProblemException - if the process cannot be started or reading the output failed
    • exec

      public static void exec(ProcessBuilder pb) throws BuildProblemException
      Executes the process and waits for the termination. If the exit cade is not 0, an exception is thrown
      Parameters:
      pb - to execute
      Throws:
      BuildProblemException - if the exit code of the process is not 0 are problems appear when starting the process
    • exec

      public static int exec(ProcessBuilder pb, Consumer<Process> afterStart) throws BuildProblemException
      Starts the process defined by the process builder, then applies the given consumer and then waits for the process to teminate.
      Parameters:
      pb - defines the process to start
      afterStart - is called with the process object after the process was started. This allows, for example, to provide input for the process (if the builder prepared for it)
      Returns:
      the exit code of the process
      Throws:
      BuildProblemException