Package adams.core.management
Class ProcessUtils
- java.lang.Object
-
- adams.core.management.ProcessUtils
-
public class ProcessUtils extends Object
A helper class for process related stuff.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
-
-
Field Summary
Fields Modifier and Type Field Description static longAUTO_PIDthe constant for the auto PID.static longNO_PIDthe constant for no PID.static booleanSHUTTING_DOWNglobal flag whether the JVM is shutting down.
-
Constructor Summary
Constructors Constructor Description ProcessUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description protected static String[]convertHashMap(HashMap<String,String> env)Converts the environment variables stored in the hashmap to a string array ("key=value").static StringescapeBlanks(String path)Escapes blanks in the path for Windows using the caret ("^").static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String cmd)Executes the command and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String[] cmd)Executes the commandline array and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String[] cmd, PlaceholderDirectory cwd)Executes the commandline array and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String[] cmd, String[] env, PlaceholderDirectory cwd)Executes the commandline array and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String[] cmd, String[] env, String input, PlaceholderDirectory cwd)Executes the commandline array and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String[] cmd, HashMap<String,String> env, PlaceholderDirectory cwd)Executes the commandline array and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String[] cmd, HashMap<String,String> env, String input, PlaceholderDirectory cwd)Executes the commandline array and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String cmd, PlaceholderDirectory cwd)Executes the command and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String cmd, String[] env, PlaceholderDirectory cwd)Executes the command and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String cmd, String[] env, String input, PlaceholderDirectory cwd)Executes the commandline array and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String cmd, HashMap<String,String> env, PlaceholderDirectory cwd)Executes the command and returns a result container.static com.github.fracpete.processoutput4j.output.CollectingProcessOutputexecute(String cmd, HashMap<String,String> env, String input, PlaceholderDirectory cwd)Executes the command and returns a result container.static String[]flattenEnvironment(HashMap<String,String> env)Returns a flattened environment with key=value pairs.static intgetAvailableProcessors()Returns the number of available processors.static HashMap<String,String>getEnvironment()Returns the system's environment variables.static HashMap<String,String>getEnvironment(BaseKeyValuePair[] envVars, boolean nullIfEmpty)Returns the system's environment variables with the provided ones overlayed on top.static longgetVirtualMachinePID()Returns the PID of the virtual machine.static StringtoErrorOutput(com.github.fracpete.processoutput4j.output.CollectingProcessOutput output)Returns an error output based on the information stored.
-
-
-
Field Detail
-
AUTO_PID
public static final long AUTO_PID
the constant for the auto PID.- See Also:
- Constant Field Values
-
NO_PID
public static final long NO_PID
the constant for no PID.- See Also:
- Constant Field Values
-
SHUTTING_DOWN
public static boolean SHUTTING_DOWN
global flag whether the JVM is shutting down.
-
-
Method Detail
-
getVirtualMachinePID
public static long getVirtualMachinePID()
Returns the PID of the virtual machine. Caution: it's a hack and can break anytime. Do NOT rely on it. Based on this blog entry.- Returns:
- the PID or -1 in case of an error
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String cmd) throws Exception
Executes the command and returns a result container.- Parameters:
cmd- the command to execute- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String cmd, PlaceholderDirectory cwd) throws Exception
Executes the command and returns a result container.- Parameters:
cmd- the command to executecwd- the working directory for the command, null to ignore- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String cmd, HashMap<String,String> env, PlaceholderDirectory cwd) throws Exception
Executes the command and returns a result container.- Parameters:
cmd- the command to executeenv- the environment variables, null if to inherit current onescwd- the working directory for the command, null to ignore- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String cmd, HashMap<String,String> env, String input, PlaceholderDirectory cwd) throws Exception
Executes the command and returns a result container.- Parameters:
cmd- the command to executeenv- the environment variables, null if to inherit current onesinput- the input to write to the processcwd- the working directory for the command, null to ignore- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String cmd, String[] env, PlaceholderDirectory cwd) throws Exception
Executes the command and returns a result container.- Parameters:
cmd- the command to executeenv- the environment variables, null if to inherit current onescwd- the working directory for the command, null to ignore- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String[] cmd) throws Exception
Executes the commandline array and returns a result container.- Parameters:
cmd- the commandline array to execute- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String[] cmd, PlaceholderDirectory cwd) throws Exception
Executes the commandline array and returns a result container.- Parameters:
cmd- the commandline array to executecwd- the working directory for the command, null to ignore- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String[] cmd, HashMap<String,String> env, PlaceholderDirectory cwd) throws Exception
Executes the commandline array and returns a result container.- Parameters:
cmd- the commandline array to executeenv- the environment variables, null if to inherit current onescwd- the working directory for the command, null to ignore- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String[] cmd, HashMap<String,String> env, String input, PlaceholderDirectory cwd) throws Exception
Executes the commandline array and returns a result container.- Parameters:
cmd- the commandline array to executeenv- the environment variables, null if to inherit current onesinput- the input to write to the processcwd- the working directory for the command, null to ignore- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String[] cmd, String[] env, PlaceholderDirectory cwd) throws Exception
Executes the commandline array and returns a result container.- Parameters:
cmd- the commandline array to executeenv- the environment variables, null if to inherit current onescwd- the working directory for the command, null to ignore- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String[] cmd, String[] env, String input, PlaceholderDirectory cwd) throws Exception
Executes the commandline array and returns a result container.- Parameters:
cmd- the commandline array to executeenv- the environment variables, null if to inherit current onesinput- the input to write to the processcwd- the working directory for the command, null to ignore- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
execute
public static com.github.fracpete.processoutput4j.output.CollectingProcessOutput execute(String cmd, String[] env, String input, PlaceholderDirectory cwd) throws Exception
Executes the commandline array and returns a result container.- Parameters:
cmd- the commandline array to executeenv- the environment variables, null if to inherit current onesinput- the input to write to the processcwd- the working directory for the command, null to ignore- Returns:
- the results of the proces: exit code, stdout, stderr
- Throws:
Exception- if something goes wrong
-
toErrorOutput
public static String toErrorOutput(com.github.fracpete.processoutput4j.output.CollectingProcessOutput output)
Returns an error output based on the information stored.- Returns:
- the error output
-
getAvailableProcessors
public static int getAvailableProcessors()
Returns the number of available processors.- Returns:
- the number of processors of the machine
-
getEnvironment
public static HashMap<String,String> getEnvironment()
Returns the system's environment variables. Returns the same content asSystem.getenv(), but this hashmap can be modified.- Returns:
- the environment variables as key-value pairs
-
getEnvironment
public static HashMap<String,String> getEnvironment(BaseKeyValuePair[] envVars, boolean nullIfEmpty)
Returns the system's environment variables with the provided ones overlayed on top.- Parameters:
envVars- the environment variables to overlaynullIfEmpty- if true and no custom environment variables, then returns null- Returns:
- the environment variables as key-value pairs or null if nullIfEmpty is true and no custom environment variables
-
flattenEnvironment
public static String[] flattenEnvironment(HashMap<String,String> env)
Returns a flattened environment with key=value pairs.- Parameters:
env- the environment to flatten, can be null- Returns:
- the flattened environment
-
convertHashMap
protected static String[] convertHashMap(HashMap<String,String> env)
Converts the environment variables stored in the hashmap to a string array ("key=value"). If the hashmap is null, null is returned as well.- Parameters:
env- the hashmap to convert- Returns:
- the generated string array, null if hashmap was null
-
-