Java – Invoke command-line tools internally through Java applications

Invoke command-line tools internally through Java applications… here is a solution to the problem.

Invoke command-line tools internally through Java applications

I

have a command line tool and I want to launch it from a Java application. Then my app should wait for the command line tool to return/finish.
I will deploy my application on Windows, Mac, and Linux, and my application should be able to invoke command-line tools on each platform.
How do I properly call it from my Java application?

Solution

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Process p = pb.start();
p.waitFor();

Related Problems and Solutions