import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class CmndExec { public static void main(String[] args) { String[] cmdArr = new String[]{"CQperl", "-e", "print \"Hello Elisheva\""}; System.out.println("Exit status: " + execute(cmdArr)); } public static int execute(String[] cmdArr) { int returnValue = -1; if (cmdArr != null && cmdArr.length > 0) { Process p = null; Runtime rt = Runtime.getRuntime(); try { p = rt.exec(cmdArr); // throws IOException returnValue = p.waitFor(); // throws InterruptedException } catch (IOException xIo) { throw new RuntimeException("Error executing command.", xIo); } catch (InterruptedException xInterrupted) { throw new RuntimeException("Command execution interrupted.", xInterrupted); } InputStreamReader isr = new InputStreamReader(p.getInputStream()); BufferedReader stdout = null; stdout = new BufferedReader(isr); String line = null; try { while ((line = stdout.readLine()) != null) { System.out.println(line); } } catch (IOException xIo) { throw new RuntimeException("Error reading process output", xIo); } } return returnValue; } }