Advertisement
richarduie

CommandExecutor.java

Jun 25th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.io.IOException;
  4.  
  5. public class CommandExecutor
  6. {
  7.     /**
  8.      * Run single, external script, program, system command, etc.
  9.      * @author: richarduie [at] yahoo [dot] com
  10.      *
  11.      * Usage syntax is static call to main as:
  12.      *    CommandExecutor cmd
  13.      *      run command and ignore output of external command process
  14.      *    CommandExecutor cmd true
  15.      *      run command and capture output of external command process for
  16.      *      display in output stream of current process
  17.      *
  18.      * args[0] - command to execute
  19.      * args[1] - optional flag (true/<anything else> to indicate whether
  20.      *           to capture command output and display in ouput stream
  21.      *           for current process
  22.      *          
  23.      * NOTE: if command (args[0]) includes spaces, it must be in quotes, e.g.,
  24.      *       [CommandExecutor "ls /" true] lists all subdirectories from root
  25.      *       of a *nix installation and writes output to current stream
  26.      */
  27.  
  28.     public static void main(String[] args) {
  29.         // report usage error and exit, if no arguments
  30.         if (0 == args.length) {
  31.             System.out.println("CommandExecutor.main() - no command given");
  32.             return;
  33.         }
  34.         // if second argument given and value is String "true", capture
  35.         // output from external command output stream and write to output
  36.         // stream for this process
  37.         boolean capture = 1 < args.length && "true".equals( args[1] );
  38.         try {
  39.             // execute command - will run in separate, external
  40.             // process not directly impacting current console
  41.             Process p = Runtime.getRuntime().exec( args[0] );
  42.             // retrieve and display output stream of command process
  43.             if (capture) {
  44.                 // for read/write of stdout for command process
  45.                 String str;
  46.                 // read in process output associated with command
  47.                 BufferedReader in = new BufferedReader(
  48.                     new InputStreamReader( p.getInputStream( ) )
  49.                 );
  50.                 // write each line of external command process to
  51.                 // current console
  52.                 while ( null != (str = in.readLine( ))) {
  53.                     System.out.println( str );
  54.                 }
  55.             }
  56.         }
  57.         catch (IOException e) {
  58.             System.out.println(
  59.                 "CommandExecutor.main() - could not run command:\n" +
  60.                 args[0] + "\nException reported was:\n" + e.toString() 
  61.             );
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement