Skip to content

[Pattern] Runtime.exec(String) to ProcessBuilder arguments #207

Description

@brunoborges

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.

Metadata

Metadata

Labels

slugNew or updated pattern snippet (category/slug.json)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions