public class SystemCommand extends Object
Constructor and Description |
---|
SystemCommand(File workdir,
String... command)
Creates a command.
|
SystemCommand(String... command)
Creates a command for which the execution will be in the working
directory of the current process.
|
Modifier and Type | Method and Description |
---|---|
void |
abort()
Aborts the running command.
|
void |
addErrorListener(IInputStreamListener listener)
Adds an error (STDERR) listener to this system command.
|
void |
addOutputListener(IInputStreamListener listener)
Adds an output (STDOUT) listener to this system command.
|
static void |
escape(List<String> command)
Escapes spaces in each parts of the command as well as special
characters in some operating systems, if they are not already
escaped.
|
static String[] |
escape(String... command)
Escapes spaces in each parts of the command as well as special
characters in some operating systems, if they are not already
escaped.
|
int |
execute()
Executes this system command and returns only when the underlying
process stopped running.
|
int |
execute(boolean runInBackground)
Executes this system command.
|
int |
execute(InputStream input)
Executes this system command with the given input and returns only when
the underlying process stopped running.
|
int |
execute(InputStream input,
boolean runInBackground)
Executes this system command with the given input.
|
String[] |
getCommand()
Gets the command to be run.
|
Map<String,String> |
getEnvironmentVariables()
Gets environment variables.
|
File |
getWorkdir()
Gets the command working directory.
|
boolean |
isRunning()
Returns whether the command is currently running.
|
void |
removeErrorListener(IInputStreamListener listener)
Removes an error (STDERR) listener.
|
void |
removeOutputListener(IInputStreamListener listener)
Removes an output (STDOUT) listener.
|
void |
setEnvironmentVariables(Map<String,String> environmentVariables)
Sets environment variables.
|
String |
toString()
Returns the command to be executed.
|
public SystemCommand(String... command)
command
- the command to runpublic SystemCommand(File workdir, String... command)
command
- the command to runworkdir
- command working directory.public String[] getCommand()
public File getWorkdir()
public void addErrorListener(IInputStreamListener listener)
listener
- command error listenerpublic void removeErrorListener(IInputStreamListener listener)
listener
- command error listenerpublic void addOutputListener(IInputStreamListener listener)
listener
- command output listenerpublic void removeOutputListener(IInputStreamListener listener)
listener
- command output listenerpublic Map<String,String> getEnvironmentVariables()
public void setEnvironmentVariables(Map<String,String> environmentVariables)
null
(default) for the
command to inherit the environment of the current process.environmentVariables
- environment variablespublic boolean isRunning()
true
if runningpublic void abort()
public int execute() throws SystemCommandException
SystemCommandException
- problem executing commandpublic int execute(boolean runInBackground) throws SystemCommandException
runInBackground
- true
to runs the system command in
background.SystemCommandException
- problem executing commandIllegalStateException
- when command is already runningpublic int execute(InputStream input) throws SystemCommandException
input
- process input (fed to STDIN)SystemCommandException
- problem executing commandpublic int execute(InputStream input, boolean runInBackground) throws SystemCommandException
input
- process input (fed to STDIN)runInBackground
- true
to runs the system command in
background.SystemCommandException
- problem executing commandIllegalStateException
- when command is already runningpublic String toString()
public static void escape(List<String> command)
command
- the command to escape.public static String[] escape(String... command)
command
- the command to escape.Copyright © 2008–2021 Norconex Inc.. All rights reserved.