Java Runtime.getRuntime(): getting output from executing a command line program
Here's a bare-bones way to nab an output from any command line program in Java. We'll use Runtime.exec()
, read from the program's InputStream
with a BufferedReader
, and process the output.
Process management & Wrangling outputs
Working with external processes can feel like dealing with aliens. You don't know their language, you don't know their technology, but you've got to somehow communicate. Here's how you do it without causing an intergalactic incident:
Command and control with ProcessBuilder
This ain't your grandpa's Runtime.exec()
. ProcessBuilder
lets you play boss and exercise more control over the process:
Streamlined Output Management
Don't let your streams cross! To avoid a buffer overflow ghost, use the snapshot rule - read the output and the error simultaneously:
Converting & Parsing Output
To easy your journey in the process wilderness, there's the ever-reliable IOUtils
from Apache commons-io to convert the output stream to a String:
Manual Execution & Permissions
Sure, it's a manual car, but do you know how to drive it? Test your command outside of Java to ensure it runs succesfully and your Java application has allowed permissions.
Stream Management & Character Encoding
Like a river, streams flow effortlessly... if you manage them right! Remember - stream management is not a stream of consciousness. It requires planning. Here's how:
Encoding: Lost in Translation?
Just as you won't understand French if you don't know the language, use InputStreamReader
with the correct character encoding to prevent your output from becoming gibberish:
Stream Buffers: The Leaky Faucet Problem
Streams can overflow easily. If you don't want to call plumber Java to your rescue, watch the buffer size carefully:
Resource Management: Cleaning After Yourself
Nobody likes a memory hog. With try-with-resources, Java 7+ does the clean-up duty for you:
Handling Errors: Oopsie-daisies!
No one likes errors! But they do happen. Keeping tabs on Process.getErrorStream()
helps you handle the unexpected:
Navigating the edge cases
In the hinterland of processes, you may trip over the unexpected. Let's identify these booby traps:
The Stream Deadlocks
For streaming outputs longer than a Tolstoy novel, consider asynchronous read or PipedInputStream/PipedOutputStream. Safety first!
Handling Ugly Processes
What's more restless than a toddler hopped up on sugar? A long-running process! Handle them with grace:
Robust Error Handling & Testing
Your code should run like a seasoned globe trotter - well in all environments and with all command-line tools.
Was this article helpful?