I want to execute Bash commands from within a Java program running on a Windows desktop. I installed Cygwin, and I have found the following code:
public class BashRunner {
private static final Logger log = Logger.getLogger(BashRunner.class.getName());
public static void run(String comand) throws IOException, InterruptedException {
Runtime run = Runtime.getRuntime();
String[] env = new String[] {"path=%PATH%;C:/cygwin/bin/"};
Process proc = run.exec(new String[]{"bash.exe", comand}, env);
proc.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while (br.ready()) {
System.out.println(br.readLine());
}
}
}
The problem is that the process enters an infinite loop...
The BashRunner.run(String)
method is called from the main
method of my program like so:
BashRunner.run("ls -alt");