GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — Runtime.exec(String) to ProcessBuilder arguments | java.evolved
Tooling
Intermediate
Runtime.exec(String) to ProcessBuilder argumentsLaunch processes with an explicit argument list and ProcessBuilder configuration. Code Comparison
✕ Command string
Process process = Runtime.getRuntime()
.exec("git show " + revision);
✓ ProcessBuilder
Process process = new ProcessBuilder(
"git", "show", revision)
.redirectErrorStream(true)
.start();
See a problem with this code? Let us know.
Why the modern way wins
🎯
Exact argumentsEach process argument remains a distinct value without command-string tokenization. 🎛
Explicit configurationEnvironment, directory, redirects, and error handling are configured together. 🛡
Safer boundariesAvoids 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
Proof
Related patterns
Concurrency
Intermediate
Modern Process APIJava 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()));
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);
JDK 9+
→
Security
Advanced
SecurityManager checks to explicit authorizationJDK 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);
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. |