java.lang.Object
net.i2p.util.ShellCommand

public class ShellCommandextends Object
Passes a command to the OS shell for execution and manages the input and output.
  • Constructor Details

    • ShellCommand

      public ShellCommand()
      No command is running; each execute* call starts its own CommandThread and waits for it, so nothing is pending at construction.
  • Method Details

    • executeSilentAndWait

      public boolean executeSilentAndWait(String[] commandArray)
      Passes a command to the shell for execution. This method blocks until all of the command's resulting shell processes have completed. Any output produced by the executed command will not be displayed.
      Parameters:
      commandArray - The command for the shell to execute, as a String[]. See Runtime.exec(String[]) for more info.
      Returns:
      true if the spawned shell process returns an exit status of 0 (indicating success), else false.
    • executeSilentAndWaitTimed

      public boolean executeSilentAndWaitTimed(String shellCommand, int seconds)
      Passes a command to the shell for execution. This method blocks until all of the command's resulting shell processes have completed unless a specified number of seconds has elapsed first. Any output produced by the executed command will not be displayed.

      Warning, no good way to quote or escape spaces in arguments when shellCommand is a String. Use a String array for best results, especially on Windows.

      Parameters:
      shellCommand - The command for the shell to execute, as a String. You can't quote arguments successfully. See Runtime.exec(String) for more info.
      seconds - The method will return true if this number of seconds elapses without the process returning an exit status. A value of 0 here disables waiting.
      Returns:
      true if the spawned shell process returns an exit status of 0 (indicating success), OR if the time expires, else false.
    • executeSilentAndWaitTimed

      public boolean executeSilentAndWaitTimed(String[] commandArray, int seconds)
      Passes a command to the shell for execution. This method blocks until all of the command's resulting shell processes have completed unless a specified number of seconds has elapsed first. Any output produced by the executed command will not be displayed.
      Parameters:
      commandArray - The command for the shell to execute, as a String[]. See Runtime.exec(String[]) for more info.
      seconds - The method will return true if this number of seconds elapses without the process returning an exit status. A value of 0 here disables waiting.
      Returns:
      true if the spawned shell process returns an exit status of 0 (indicating success), OR if the time expires, else false.
    • executeSilentAndWaitTimed

      public boolean executeSilentAndWaitTimed(String[] commandArray, String[] envp, int seconds)
      Execute the command array with an explicit environment, waiting for completion with a timeout, e.g. to hand a password to keytool via -storepass:env instead of the visible command line.
      Parameters:
      commandArray - the command and its arguments
      envp - environment entries as NAME=value strings, replacing the inherited environment (include those entries you want to keep), or null to inherit it
      seconds - the maximum time to wait, in seconds
      Returns:
      true if the spawned process returns an exit status of 0 (indicating success), OR if the time expires, else false
    • executeSAWT

      private boolean executeSAWT(Object shellCommand, String[] envp, int seconds)
      Execute the command, timing it for the debug log.
      Parameters:
      shellCommand - a command string or a String[] command array
      envp - the environment for the command, or null to inherit the current one
      seconds - how long to wait for the command to finish, 0 or less to wait forever
      Returns:
      true if the command succeeded or the wait timed out, false if it failed
    • main

      public static void main(String[] args)
      Just does exec, this is NOT a test of ShellCommand.
      Parameters:
      args - the command to run as an exec() argument array; ignored when empty, which prints the usage line instead
    • execute

      private boolean execute(Object shellCommand, String[] envp)
      Executes the command with an explicit environment and waits for it to complete. The process is destroyed if the wait is interrupted.
      Parameters:
      shellCommand - either a String or a String[] (since 0.8.3) - quick hack
      envp - environment entries as NAME=value strings, or null to inherit
      Returns:
      true if the process exited with status 0, else false