process-api: ProcessBuilder and Process API module (pipe-buffer deadlock and its fixes, timeouts and process-tree kill, grandchild holding the pipe, environment/redirect/stdin, ProcessHandle, startPipeline, shell and option injection), ProcessRunner, 14 contract tests

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b3W3uHbHAmM3orhn7b
This commit is contained in:
Ankur
2026-11-26 00:48:44 +05:30
co-authored by Claude Sonnet 5.5
parent 47e4ccbefa
commit a714780bca
21 changed files with 952 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
# 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
```bash
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.