com.denova.runtime
Class Exec

java.lang.Object
  |
  +--com.denova.runtime.Exec

public class Exec
extends java.lang.Object

Provides methods to execute a command.

Author:
DeNova Copyright © 1997-2008 DeNova All rights reserved worldwide.

Field Summary
static java.lang.String UnixCommandFilename
          UnixCommandFilename is a temporary file called .tempjcmd.
static java.lang.String WindowsCommandFilename
          WindowsCommandFilename is a temporary file called _tempcmd.bat.
 
Constructor Summary
Exec()
           
 
Method Summary
static int execCommand(java.lang.String programCommand)
          Executes a program that uses stdin, stdout, and stderr.
static int execCommand(java.lang.String[] commandArray)
          Executes a program that uses stdin, stdout, and stderr.
static java.util.List getCommandArgs(java.lang.String command)
          Gets the command in an array list.
static java.lang.String[] getCommandAsStringArray(java.lang.String command)
          Gets the command array.
static java.lang.String getCommandOutput(java.lang.String command)
          Executes a command and returns the command's standard output as a String.
static java.lang.String getCommandOutput(java.lang.String[] command)
          Executes a command and returns the command's standard output as a String.
static java.lang.String getWindowsCommand(java.lang.String command, java.lang.String directory)
           
static boolean hasWhiteSpace(java.lang.String arg)
          Returns true if the arg has one or more white space characters.
static boolean isWhiteSpace(char c)
          Returns true if the char is a white space character.
static void launchCommand(java.lang.String command)
          Executes a command; strongly recommend you use one of the other runCommand methods.
static void launchCommand(java.lang.String command, java.lang.String directory)
          Executes a command from a directory.
static void main(java.lang.String[] args)
          Used to test running a command on a target system.
static java.lang.String quoteArg(java.lang.String arg)
          If the arg has one or more white space characters and doesn't already have quotes, add them.
static void runCommand(java.lang.String command)
          Executes a command; strongly recommend you use one of the other runCommand methods.
static void runCommand(java.lang.String[] command)
          Executes a command.
static void runCommand(java.lang.String command, java.io.File directory)
          Executes a command from a directory.
static void runCommand(java.lang.String command, java.lang.String directory)
          Executes a command from a directory.
static void startAppWithoutWaiting(java.lang.String command)
          Executes a command and does *not* wait for the process to start or finish.
static java.lang.String unquoteArg(java.lang.String arg)
          If the arg starts and ends with quotes, remove them
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WindowsCommandFilename

public static final java.lang.String WindowsCommandFilename
WindowsCommandFilename is a temporary file called _tempcmd.bat.

See Also:
Constant Field Values

UnixCommandFilename

public static final java.lang.String UnixCommandFilename
UnixCommandFilename is a temporary file called .tempjcmd.

See Also:
Constant Field Values
Constructor Detail

Exec

public Exec()
Method Detail

execCommand

public static int execCommand(java.lang.String programCommand)
                       throws java.lang.Exception
Executes a program that uses stdin, stdout, and stderr.

The programCommand must start with the program name to run plus any command line arguments, if needed. The programCommand may not a built-in OS command, such as dir on a Windows.

Parameters:
programCommand - the full command line, including arguments to be executed
Returns:
runtime process' exit value.
Throws:
java.lang.Exception - any unexpected event

execCommand

public static int execCommand(java.lang.String[] commandArray)
                       throws java.lang.Exception
Executes a program that uses stdin, stdout, and stderr.

The first argument must start with the program name, including the path, to run. The first argument may not a built-in OS command, such as dir on a Windows.

Parameters:
commandArray - the program name and arguments as separate items
Returns:
runtime process' exit value.
Throws:
java.lang.Exception - any unexpected event

runCommand

public static void runCommand(java.lang.String command,
                              java.lang.String directory)
                       throws java.lang.Exception
Executes a command from a directory.

If the command is issued on a Windows or Unix system, then a batch file or script is created which changes to directory, executes the command, moves back to the original directory, and erases the batch/script file.

Parameters:
command - the full command line, including arguments to be executed
directory - the directory where the command will be launched
Throws:
java.lang.Exception - any unexpected event

runCommand

public static void runCommand(java.lang.String command,
                              java.io.File directory)
                       throws java.lang.Exception
Executes a command from a directory.

If the command is issued on a Windows or Unix system, then a batch file or script is created which changes to directory, executes the command, moves back to the original directory, and erases the batch/script file.

Parameters:
command - full command line, including arguments to be executed
directory - directory where the command will be launched
Throws:
java.lang.Exception - any unexpected event

runCommand

public static void runCommand(java.lang.String command)
                       throws java.lang.Exception
Executes a command; strongly recommend you use one of the other runCommand methods.

Parameters:
command - full command line, including arguments to be executed
Throws:
java.lang.Exception - any unexpected event

runCommand

public static void runCommand(java.lang.String[] command)
                       throws java.lang.Exception
Executes a command.

Parameters:
command - the program name and arguments as separate items
Throws:
java.lang.Exception - any unexpected event

getCommandOutput

public static java.lang.String getCommandOutput(java.lang.String command)
                                         throws java.lang.Exception
Executes a command and returns the command's standard output as a String.

Parameters:
command - full command line, including arguments to be executed
Returns:
command output
Throws:
java.lang.Exception - any unexpected event

getCommandOutput

public static java.lang.String getCommandOutput(java.lang.String[] command)
                                         throws java.lang.Exception
Executes a command and returns the command's standard output as a String.

Parameters:
command - program name and arguments as separate items
Returns:
command output
Throws:
java.lang.Exception - any unexpected event

launchCommand

public static void launchCommand(java.lang.String command,
                                 java.lang.String directory)
                          throws java.lang.Exception
Executes a command from a directory.

If the command is issued on a Windows or Unix system, then a batch file or script is created which changes to directory, executes the command, moves back to the original directory, and erases the batch/script file.

This command differs from runCommand because instead of calling p. waitFor() on Windows, it looks at the stdout for the EndOfStreamToken. This method handles a bug in some cmd.exe on WindowsNT. Important: The command that is being run through launchCommand must write to stdout or stderr for this method to work.

Parameters:
command - full command line, including arguments to be executed
directory - directory where the command will be launched
Throws:
java.lang.Exception - any unexpected event

launchCommand

public static void launchCommand(java.lang.String command)
                          throws java.lang.Exception
Executes a command; strongly recommend you use one of the other runCommand methods.

This command differs from runCommand because instead of calling p. waitFor() on Windows, it looks at the stdout for the EndOfStreamToken. This method handles a bug in some cmd.exe on WindowsNT. Important: The command that is being run through launchCommand must write to stdout or stderr for this method to work.

Parameters:
command - full command line, including arguments to be executed
Throws:
java.lang.Exception - any unexpected event

startAppWithoutWaiting

public static void startAppWithoutWaiting(java.lang.String command)
                                   throws java.lang.Exception
Executes a command and does *not* wait for the process to start or finish.

Parameters:
command - full command line, including arguments to be executed
Throws:
java.lang.Exception - any unexpected event

getCommandAsStringArray

public static java.lang.String[] getCommandAsStringArray(java.lang.String command)
                                                  throws java.lang.Exception
Gets the command array.

Parameters:
command -
Returns:
string array of commands line args.
Throws:
java.lang.Exception - any unexpected event

getCommandArgs

public static java.util.List getCommandArgs(java.lang.String command)
                                     throws java.lang.Exception
Gets the command in an array list. Old JVMs would not let you quote an entire classpath if it contained white space. Instead you had to quote just the path element, e.g.
      a.jar:"b c.jar":d.jar
  
Although newer JVMs don't require this quirk, there are a lot of classpaths that use the old quoting. New JVMs still accept it, and so we have to, too.

Parameters:
command -
Returns:
array list of the command line args.
Throws:
java.lang.Exception - any unexpected event

getWindowsCommand

public static java.lang.String getWindowsCommand(java.lang.String command,
                                                 java.lang.String directory)

main

public static void main(java.lang.String[] args)
Used to test running a command on a target system.

Parameters:
args - The command line arguments

quoteArg

public static java.lang.String quoteArg(java.lang.String arg)
If the arg has one or more white space characters and doesn't already have quotes, add them. We assume that if the arg already starts with a quote, then it is already quoted and it also ends with a quote. Args are quoted using a single quote character because embedded double quotes are more common in command line arguments than embedded single quotes. Embedded single quotes are not detected.

Parameters:
arg - command line argument
Returns:
quoted command line argument, or unquoted arg if no white space

unquoteArg

public static java.lang.String unquoteArg(java.lang.String arg)
If the arg starts and ends with quotes, remove them

Parameters:
arg - command line argument
Returns:
unquoted arg if no white space

hasWhiteSpace

public static boolean hasWhiteSpace(java.lang.String arg)
Returns true if the arg has one or more white space characters. White space is ' ', '\t', '\r', or '\n'.

Parameters:
arg - command line argument
Returns:
true if the arg has any white space

isWhiteSpace

public static boolean isWhiteSpace(char c)
Returns true if the char is a white space character. White space is ' ', '\t', '\r', or '\n'.

Parameters:
c - character to check
Returns:
true if character has any white space