Category
tooling
Slug
runtime-exec-to-process-builder
Title
Runtime.exec(String) to ProcessBuilder arguments
Difficulty
intermediate
Since JDK
5
Summary
Launch processes with an explicit argument list and ProcessBuilder configuration.
Old code label
Command string
Old code
Process process = Runtime.getRuntime()
.exec("git show " + revision);
Modern code label
ProcessBuilder
Modern code
Process process = new ProcessBuilder(
"git", "show", revision)
.redirectErrorStream(true)
.start();
Explanation
Runtime.exec(String) applies Java's command-string tokenization, which is easy to misunderstand when arguments contain spaces or quoting. ProcessBuilder accepts program arguments as distinct values and exposes the working directory, environment, input/output redirects, and error-stream policy explicitly. It does not invoke a shell unless the application deliberately launches one.
Why the modern way wins
🎯 Exact arguments — Each process argument remains a distinct value without command-string tokenization.
🎛 Explicit configuration — Environment, directory, redirects, and error handling are configured together.
🛡 Safer boundaries — Avoids constructing a shell-like command string from dynamic values.
Category
tooling
Slug
runtime-exec-to-process-builder
Title
Runtime.exec(String) to ProcessBuilder arguments
Difficulty
intermediate
Since JDK
5
Summary
Launch processes with an explicit argument list and ProcessBuilder configuration.
Old code label
Command string
Old code
Modern code label
ProcessBuilder
Modern code
Explanation
Runtime.exec(String) applies Java's command-string tokenization, which is easy to misunderstand when arguments contain spaces or quoting. ProcessBuilder accepts program arguments as distinct values and exposes the working directory, environment, input/output redirects, and error-stream policy explicitly. It does not invoke a shell unless the application deliberately launches one.
Why the modern way wins
🎯 Exact arguments — Each process argument remains a distinct value without command-string tokenization.
🎛 Explicit configuration — Environment, directory, redirects, and error handling are configured together.
🛡 Safer boundaries — Avoids constructing a shell-like command string from dynamic values.