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");
Janus Varmarken
2,3463 gold badges20 silver badges43 bronze badges
-
Where does it enter an infinite loop? Does it print anything? More details pleasemckuok– mckuok2018年08月09日 03:57:36 +00:00Commented Aug 9, 2018 at 3:57
-
it seems like it remains stuck at this line "proc.waitFor();".... String[] env = new String[]{"Path=%PATH%;C:/cygwin/bin/"}; Process proc = run.exec(new String[]{"bash.exe", "-c" , comand}, env); so i guess at this two line i screwed up something...Gabriel Mutis– Gabriel Mutis2018年08月09日 08:27:35 +00:00Commented Aug 9, 2018 at 8:27
-
stackoverflow.com/questions/26830617/java-running-bash-commands try out thisBHARATHWAJ– BHARATHWAJ2018年08月09日 08:54:35 +00:00Commented Aug 9, 2018 at 8:54
default