Class Executor

java.lang.Object
com.cedarsoftware.util.Executor

public class Executor extends Object
A utility class for executing system commands and capturing their output.

This class provides a convenient wrapper around Java's Runtime.exec(String) methods, capturing both standard output and standard error streams. It handles stream management and process cleanup automatically.

Features:

  • Executes system commands with various parameter combinations
  • Captures stdout and stderr output
  • Supports environment variables
  • Supports working directory specification
  • Non-blocking output handling

Example Usage:


 Executor exec = new Executor();
 int exitCode = exec.exec("ls -l");
 String output = exec.getOut();      // Get stdout
 String errors = exec.getError();    // Get stderr
 
Author:
John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    exec(String command)
    Executes a command using the system's runtime environment.
    int
    exec(String[] cmdarray)
    Executes a command array using the system's runtime environment.
    int
    exec(String[] cmdarray, String[] envp)
    Executes a command array with specified environment variables.
    int
    exec(String[] cmdarray, String[] envp, File dir)
    Executes a command array with specified environment variables and working directory.
    int
    exec(String command, String[] envp)
    Executes a command with specified environment variables.
    int
    exec(String command, String[] envp, File dir)
    Executes a command with specified environment variables and working directory.
    Returns the content written to standard error by the last executed command.
    Returns the content written to standard output by the last executed command.

    Methods inherited from class java.lang.Object

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

    • Executor

      public Executor()
  • Method Details

    • exec

      public int exec(String command)
      Executes a command using the system's runtime environment.
      Parameters:
      command - the command to execute
      Returns:
      the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
    • exec

      public int exec(String[] cmdarray)
      Executes a command array using the system's runtime environment.

      This version allows commands with arguments to be specified as separate array elements, avoiding issues with argument quoting and escaping.

      Parameters:
      cmdarray - array containing the command and its arguments
      Returns:
      the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
    • exec

      public int exec(String command, String[] envp)
      Executes a command with specified environment variables.
      Parameters:
      command - the command to execute
      envp - array of strings, each element of which has environment variable settings in format name=value, or null if the subprocess should inherit the environment of the current process
      Returns:
      the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
    • exec

      public int exec(String[] cmdarray, String[] envp)
      Executes a command array with specified environment variables.
      Parameters:
      cmdarray - array containing the command and its arguments
      envp - array of strings, each element of which has environment variable settings in format name=value, or null if the subprocess should inherit the environment of the current process
      Returns:
      the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
    • exec

      public int exec(String command, String[] envp, File dir)
      Executes a command with specified environment variables and working directory.
      Parameters:
      command - the command to execute
      envp - array of strings, each element of which has environment variable settings in format name=value, or null if the subprocess should inherit the environment of the current process
      dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process
      Returns:
      the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
    • exec

      public int exec(String[] cmdarray, String[] envp, File dir)
      Executes a command array with specified environment variables and working directory.
      Parameters:
      cmdarray - array containing the command and its arguments
      envp - array of strings, each element of which has environment variable settings in format name=value, or null if the subprocess should inherit the environment of the current process
      dir - the working directory of the subprocess, or null if the subprocess should inherit the working directory of the current process
      Returns:
      the exit value of the process (0 typically indicates success), or -1 if an error occurred starting the process
    • getError

      public String getError()
      Returns the content written to standard error by the last executed command.
      Returns:
      the stderr output as a string, or null if no command has been executed
    • getOut

      public String getOut()
      Returns the content written to standard output by the last executed command.
      Returns:
      the stdout output as a string, or null if no command has been executed