Files
java-core-examples/process-api/README.md
T

1.6 KiB

process-api

Companion code for the ankurm.com post "Running Shell Commands from Java Safely: ProcessBuilder, Process API and Timeouts." Module process-api in java-core-examples.

All explanation lives in the post; this module holds the runnable evidence and the captured output.

Versions

Component Version
JDK 25.0.5.1+2 (Temurin, LTS)
JUnit Jupiter 5.11.0
OS Linux (the demos shell out to sh, sleep, head, tr, cat, wc, ls, sort, uniq)

Quickstart

export JDK25_HOME=/path/to/jdk-25
./scripts/run-all.sh        # rebuilds, runs the tests, regenerates everything in output/

What is in here

File Shows Output
BasicsDemo the smallest correct use; failing command vs missing binary; exitValue() too early; no shell 01
DeadlockDemo the 64 KiB pipe deadlock, the stderr variant, and three fixes 02
TimeoutDemo waitFor(timeout), destroy() vs destroyForcibly(), orphaned grandchildren, a grandchild holding the pipe 03
EnvironmentDemo environment copy, clear(), which PATH finds the program, working directory, redirects, stdin 04
HandleDemo ProcessHandle, Info, onExit(), startPipeline 05
InjectionDemo shell-string injection, option injection, --, an allow-list 06
ProcessRunner the safe runner: concurrent drain, capped capture, closed stdin, deadline, tree kill used by 02, 03
ProcessClaimsTest 14 assertions behind the claims above 07

Child PIDs and timings vary between runs; the behaviour (what hangs, what exits, which exit code) does not.