Which one is better? By better I mean which one has better security, etc. (not ease of use).
ProcessBuilder is not thread safe, as stated in the javadoc.
This class is used to create operating system processes. Each ProcessBuilder instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes.
exec(String command) method executes the specified string command in a separate process. This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation exec(command, null, null).
You can call destroy() method on it.
Ease of use is the only real difference between those two.
Note that ease of use can lead to security by helping to avoid mis-use.
At least on OpenJDK 6 Runtime.exec()
is implemented using ProcessBuilder
:
public Process exec(String[] cmdarray, String[] envp, File dir)
throws IOException {
return new ProcessBuilder(cmdarray)
.environment(envp)
.directory(dir)
.start();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With