The library SystemProcess has several object methods as well as some static methods
to start a system process, to attribute settings (e.g. CPU affinity), get its PID, destroy, check its current status, to get a list of all running processes.
The attached source code file SystemProcessExample.java seems to be self-explanatory. It shows a few expected use cases of the library.
SystemProcess(List<String> commandArgs)
creates an object without actually starting the process.
0th String in commandArgs
is the executable name.
int pid = SystemProcess.start(String pname, boolean destroyIfUnknownPID)
starts the process with trying to get its PID.
destroyIfUnknownPID
defines whether to destroy a process whose PID could not be retrieved.
SystemProcessException
with "process id cannot be defined" message will be thrown in any case, no matter what destroyIfUnknownPID
was.
This method is a workaround for earlier Java versions (before 9.0) which have no built-in method to get the PID.
An easier way to start a process without getting its PID is
void SystemProcess.start()
void SystemProcess.waitForSeconds(int seconds)
and similar methods wait either for the process to finish or for the timeout to expire.
The calling thread is blocked.
String SystemProcess.readLine()
and similar methods all wait for the process to start (if needed) blocking the calling thread and then return a line or an integer from the process output stream.
boolean SystemProcess.setProcessPriority(int priority)
is not implemented yet.
boolean SystemProcess.setProcessAffinity(char[] aff)
returns true
if the requested affinity has been set and false
otherwise.
For example, affinity '0001' will assign the process to the 4th CPU.
Static method boolean setProcessAffinity(int pid, char[] aff)
does the same for a process with a known PID.
Other static methods are:
int getCPUnumber()
- to get the number of CPUs assuming hyperthreading,
HashMap<Integer, String> getProcesses()
- to get all processes on a machine,
ArrayList<Integer> getProcessIDs(String pname, boolean printExceptionMsg)
- to get PIDs of all specific processes (e.g, "chrome.exe")
void checkRMIregistry()
- checks whether RMI registry is running on a machine and tries to start it if not.