0

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,306
  • 3
  • 20
  • 42
  • Where does it enter an infinite loop? Does it print anything? More details please – mckuok Aug 09 '18 at 03: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 Aug 09 '18 at 08:27
  • https://stackoverflow.com/questions/26830617/java-running-bash-commands try out this – BHARATHWAJ Aug 09 '18 at 08:54

0 Answers0