Executing an External Application in Java

There are many times that you might like to launch an application that is running on the user's machine. For example, an external Web browser could be used to display help pages. There is a way to do this that is as easy as running it yourself from the command line. All you have to know is its name on the system. To make your Java application open Windows Explorer, write

try {
Runtime.getRuntime().exec("explorer");
} catch(IOException ioe){
System.out.println(ioe.getMessage());
}

You have to catch or rethrow the IOException that this call can occasion. Note that we just pass in the system name, not including the file extension.

Passing Arguments to an External Application

Not only can we start an external ...

Get Java Garage now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.