GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Runtime.exec(String) to ProcessBuilder arguments | java.evolved

Home / Tooling / Runtime.exec(String) to ProcessBuilder arguments
Tooling Intermediate

Runtime.exec(String) to ProcessBuilder arguments

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Launch processes with an explicit argument list and ProcessBuilder configuration.

Tooling Security
Code Comparison
✕ Command string
Process process = Runtime.getRuntime()
        .exec("git show " + revision);
✓ ProcessBuilder
Process process = new ProcessBuilder(
        "git", "show", revision)
        .redirectErrorStream(true)
        .start();
Share 𝕏
See a problem with this code? Let us know.
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.

Old Approach
Runtime.exec(String)
Modern Approach
ProcessBuilder
Since JDK
5
Difficulty
Intermediate
JDK Support
Runtime.exec(String) to ProcessBuilder arguments
Available

Widely available since JDK 5 (September 2004)

How it works

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.

Related Documentation
ProcessBuilder ↗ Runtime.exec(String) ↗
Proof
View proof source ↗
Related patterns
Concurrency Intermediate

Modern Process API

Java 8
Process p = Runtime.getRuntime()
    .exec("ls -la");
int code = p.waitFor();
// no way to get PID
// no easy process info
Java 9+
ProcessHandle ph =
    ProcessHandle.current();
long pid = ph.pid();
ph.info().command()
    .ifPresent(IO::println);
ph.children().forEach(
    c -> IO.println(c.pid()));
Hover to see modern ➜
JDK 9+ →
I/O Beginner

InputStream.transferTo()

Java 8
byte[] buf = new byte[8192];
int n;
while ((n = input.read(buf)) != -1) {
    output.write(buf, 0, n);
}
Java 9+
input.transferTo(output);
Hover to see modern ➜
JDK 9+ →
Security Advanced

SecurityManager checks to explicit authorization

JDK 23 and earlier
SecurityManager manager = System.getSecurityManager();
if (manager != null) {
    manager.checkRead(path.toString());
}
return Files.readString(path);
JDK 24+
// Untrusted users must not be able to modify this tree
Path root = allowedRoot.toRealPath();
Path resolved = root.resolve(requested)
    .normalize()
    .toRealPath();
if (!resolved.startsWith(root)) {
    throw new SecurityException(
        "Path is outside the allowed root");
}
return Files.readString(resolved);
Hover to see modern ➜
JDK 24+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.