mylib
Class Exec

java.lang.Object
  extended by mylib.Exec

public class Exec
extends java.lang.Object

A class that eases the pain of running external processes from applications. Lets you run a program three ways:

  1. exec: Execute the command, returning immediately even if the command is still running. This would be appropriate for printing a file.
  2. execWait: Execute the command, but don't return until the command finishes. This would be appropriate for sequential commands where the first depends on the second having finished (e.g. javac followed by java).
  3. execPrint: Execute the command and print the output. This would be appropriate for the UNIX command ls.
Note that the PATH is not taken into account, so you must specify the full pathname to the command, and shell builtin commands will not work. For instance, on Unix the above three examples might look like:
  1. Exec.exec("/usr/ucb/lpr Some-File");
  2.         Exec.execWait("/usr/local/bin/javac Foo.java");
            Exec.execWait("/usr/local/bin/java Foo");
            
  3. Exec.execPrint("/usr/bin/ls -al");

Version:
1.0 1997
Author:
Marty Hall ( hall@apl.jhu.edu)

Field Summary
private static boolean verbose
           
 
Constructor Summary
Exec()
           
 
Method Summary
static boolean exec(java.lang.String command)
          Starts a process to execute the command.
private static boolean exec(java.lang.String command, boolean printResults, boolean wait)
           
static boolean execPrint(java.lang.String command)
          Starts a process to execute the command.
static boolean execWait(java.lang.String command)
          Starts a process to execute the command.
static boolean getVerbose()
          Will Exec print status messages?
private static void printError(java.lang.String command)
           
private static void printError(java.lang.String command, java.lang.Exception e)
           
private static void printSeparator()
           
static void setVerbose(boolean verboseFlag)
          Determines if the Exec class should print which commands are being executed, and print error messages if a problem is found.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

verbose

private static boolean verbose
Constructor Detail

Exec

public Exec()
Method Detail

setVerbose

public static void setVerbose(boolean verboseFlag)
Determines if the Exec class should print which commands are being executed, and print error messages if a problem is found. Default is true.

Parameters:
verboseFlag - true: print messages. false: don't.

getVerbose

public static boolean getVerbose()
Will Exec print status messages?


exec

public static boolean exec(java.lang.String command)
Starts a process to execute the command. Returns immediately, even if the new process is still running.

Parameters:
command - The full pathname of the command to be executed. No shell builtins (e.g. "cd") or shell meta-chars (e.g. ">") allowed.
Returns:
false if a problem is known to occur, but since this returns immediately, problems aren't usually found in time. Returns true otherwise.

execWait

public static boolean execWait(java.lang.String command)
Starts a process to execute the command. Waits for the process to finish before returning.

Parameters:
command - The full pathname of the command to be executed. No shell builtins or shell meta-chars allowed.
Returns:
false if a problem is known to occur, either due to an exception or from the subprocess returning a non-zero value. Returns true otherwise.

execPrint

public static boolean execPrint(java.lang.String command)
Starts a process to execute the command. Prints all output the command gives.

Parameters:
command - The full pathname of the command to be executed. No shell builtins or shell meta-chars allowed.
Returns:
false if a problem is known to occur, either due to an exception or from the subprocess returning a non-zero value. Returns true otherwise.

exec

private static boolean exec(java.lang.String command,
                            boolean printResults,
                            boolean wait)

printError

private static void printError(java.lang.String command,
                               java.lang.Exception e)

printError

private static void printError(java.lang.String command)

printSeparator

private static void printSeparator()