Использование ProcessBuilder
Пример:
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Process p = pb.start();
InputStream in = p.getInputStream();
OutputStream out = p.getOutputStream();
// Write to input of the program using outputstream here
...
// Read output of program from input stream here
// ...
FileOutputStream fileOut = new FileOutputStream("output.txt");
BufferedInputStream bIn = new BufferedInputStream(in);
byte buf = new byte[4096];
int count;
while ((count = bIn.read(buf)) != -1) {
fileOut.write(buf, 0, count);
}
...
fileOut.close();
bIn.close();
// Exception handling is left as an exercise for the reader :-P