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,3463 gold badges20 silver badges43 bronze badges
asked Aug 8, 2018 at 23:58
3
  • Where does it enter an infinite loop? Does it print anything? More details please Commented 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... Commented Aug 9, 2018 at 8:27
  • stackoverflow.com/questions/26830617/java-running-bash-commands try out this Commented Aug 9, 2018 at 8:54

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.