public final class ExecHelper extends Object
This class is taken from Stephen Ostermiller's example at:
http://ostermiller.org/utils/ExecHelper.java.html
It provides some useful methods for manipulating and reading in the output of
a Process
resultant from a call to Runtime.getRuntime()
without using the Process.waitFor()
method, which apparently can hang
on certain underlying systems.
Modifier and Type | Field and Description |
---|---|
static int |
INT |
Modifier and Type | Method and Description |
---|---|
static ExecHelper |
exec(String[] cmdarray)
Executes the specified command and arguments in a separate process, and
waits for the process to finish.
|
static ExecHelper |
exec(String[] cmdarray,
String charset)
Executes the specified command and arguments in a separate process, and
waits for the process to finish.
|
static ExecHelper |
exec(String[] cmdarray,
String[] envp)
Executes the specified command and arguments in a separate process, and
waits for the process to finish.
|
static ExecHelper |
exec(String[] cmdarray,
String[] envp,
File dir)
Executes the specified command and arguments in a separate process, and
waits for the process to finish.
|
static ExecHelper |
exec(String[] cmdarray,
String[] envp,
File dir,
String charset)
Executes the specified command and arguments in a separate process, and
waits for the process to finish.
|
static ExecHelper |
exec(String[] cmdarray,
String[] envp,
String charset)
Executes the specified command and arguments in a separate process, and
waits for the process to finish.
|
static ExecHelper |
execUsingShell(String command)
Executes the specified command using a shell.
|
static ExecHelper |
execUsingShell(String command,
String charset)
Executes the specified command using a shell.
|
String |
getError()
Get the error output of the job that ran.
|
String |
getOutput()
Get the output of the job that ran.
|
int |
getStatus()
Get the status of the job that ran.
|
public static final int INT
public static ExecHelper exec(String[] cmdarray) throws IOException
Output from the process is expected to be text in the system's default character set.
No input is passed to the process on STDIN.
cmdarray
- array containing the command to call and its arguments.SecurityException
- if a security manager exists and its checkExec method doesn't
allow creation of a subprocess.IOException
- -
if an I/O error occursNullPointerException
- -
if cmdarray is nullIndexOutOfBoundsException
- -
if cmdarray is an empty array (has length 0).public static ExecHelper exec(String[] cmdarray, String[] envp) throws IOException
Output from the process is expected to be text in the system's default character set.
No input is passed to the process on STDIN.
cmdarray
- array containing the command to call and its arguments.envp
- array of strings, each element of which has environment
variable settings in format name=value.SecurityException
- if a security manager exists and its checkExec method doesn't
allow creation of a subprocess.IOException
- -
if an I/O error occursNullPointerException
- -
if cmdarray is nullIndexOutOfBoundsException
- -
if cmdarray is an empty array (has length 0).public static ExecHelper exec(String[] cmdarray, String[] envp, File dir) throws IOException
Output from the process is expected to be text in the system's default character set.
No input is passed to the process on STDIN.
cmdarray
- array containing the command to call and its arguments.envp
- array of strings, each element of which has environment
variable settings in format name=value.dir
- the working directory of the subprocess, or null if the
subprocess should inherit the working directory of the current
process.SecurityException
- if a security manager exists and its checkExec method doesn't
allow creation of a subprocess.IOException
- -
if an I/O error occursNullPointerException
- -
if cmdarray is nullIndexOutOfBoundsException
- -
if cmdarray is an empty array (has length 0).public static ExecHelper exec(String[] cmdarray, String charset) throws IOException
No input is passed to the process on STDIN.
cmdarray
- array containing the command to call and its arguments.charset
- Output from the executed command is expected to be in this
character set.SecurityException
- if a security manager exists and its checkExec method doesn't
allow creation of a subprocess.IOException
- -
if an I/O error occursNullPointerException
- -
if cmdarray is nullIndexOutOfBoundsException
- -
if cmdarray is an empty array (has length 0).public static ExecHelper exec(String[] cmdarray, String[] envp, String charset) throws IOException
No input is passed to the process on STDIN.
cmdarray
- array containing the command to call and its arguments.envp
- array of strings, each element of which has environment
variable settings in format name=value.charset
- Output from the executed command is expected to be in this
character set.SecurityException
- if a security manager exists and its checkExec method doesn't
allow creation of a subprocess.IOException
- -
if an I/O error occursNullPointerException
- -
if cmdarray is nullIndexOutOfBoundsException
- -
if cmdarray is an empty array (has length 0).public static ExecHelper exec(String[] cmdarray, String[] envp, File dir, String charset) throws IOException
No input is passed to the process on STDIN.
cmdarray
- array containing the command to call and its arguments.envp
- array of strings, each element of which has environment
variable settings in format name=value.dir
- the working directory of the subprocess, or null if the
subprocess should inherit the working directory of the current
process.charset
- Output from the executed command is expected to be in this
character set.SecurityException
- if a security manager exists and its checkExec method doesn't
allow creation of a subprocess.IOException
- -
if an I/O error occursNullPointerException
- -
if cmdarray is nullIndexOutOfBoundsException
- -
if cmdarray is an empty array (has length 0).public static ExecHelper execUsingShell(String command) throws IOException
A shell should be used to execute commands when features such as file redirection, pipes, argument parsing are desired.
Output from the process is expected to be text in the system's default character set.
No input is passed to the process on STDIN.
command
- String containing a command to be parsed by the shell and
executed.SecurityException
- if a security manager exists and its checkExec method doesn't
allow creation of a subprocess.IOException
- -
if an I/O error occursNullPointerException
- -
if command is nullpublic static ExecHelper execUsingShell(String command, String charset) throws IOException
A shell should be used to execute commands when features such as file redirection, pipes, argument parsing are desired.
No input is passed to the process on STDIN.
command
- String containing a command to be parsed by the shell and
executed.charset
- Output from the executed command is expected to be in this
character set.SecurityException
- if a security manager exists and its checkExec method doesn't
allow creation of a subprocess.IOException
- -
if an I/O error occursNullPointerException
- -
if command is nullpublic String getOutput()
public String getError()
public int getStatus()
Copyright © 1999–2017 Apache OODT. All rights reserved.