How to run Linux commands in Java?

You can use java.lang.Runtime.exec to run simple code. This gives you back a Process and you can read its standard output directly without having to temporarily store the output on disk. For example, here’s a complete program that will showcase how to do it: import java.io.BufferedReader; import java.io.InputStreamReader; public class testprog { public static void … Read more

Execute external program

borrowed this shamely from here Process process = new ProcessBuilder(“C:\\PathToExe\\MyExe.exe”,”param1″,”param2″).start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; System.out.printf(“Output of running %s is:”, Arrays.toString(args)); while ((line = br.readLine()) != null) { System.out.println(line); } More information here Other issues on how to pass commands here and here

process.waitFor() never returns

There are many reasons that waitFor() doesn’t return. But it usually boils down to the fact that the executed command doesn’t quit. This, again, can have many reasons. One common reason is that the process produces some output and you don’t read from the appropriate streams. This means that the process is blocked as soon … Read more