Execute external program with Runtime and Process classes

To run an external program we need a reference to the Runtime which exists as a singleton within every Java runtime.
Executing the external program is done by calling the exec() method and it returns a reference to the process that has been created.
Now, in this case we run Notepad.exe and there won’t be any output from it written to the standard out, but in other cases there might be and it is possible to catch that information.
The Process class has a getInputStream() method that we can use to read the data, and we read for as long as the read() method doesn’t return -1.
The waitFor() method ensures that the Java program is not exited until the Process is finished. If the only goal is to start an external program then this line of code doesn’t make much sense, but if there were more Java code waiting to be executed that was dependent on the result of the process, then this method is very handy.

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?