14

Which one is better? By better I mean which one has better security, etc. (not ease of use).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
JavaIsGreat
  • 945
  • 1
  • 9
  • 10

1 Answers1

32

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();
}
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Except that `Runtime.exec` has overloads that take `command` as a single `String` and tokenize it, but in `ProcessBuilder` a single `String` is handled as a vararg `String[1]` -- see http://stackoverflow.com/questions/6856028/difference-between-processbuilder-and-runtime-exec – dave_thompson_085 May 18 '17 at 21:08