raHul raHul - 6 months ago 23
Bash Question

How to run a .sh file from Java?

I need to run a .sh file from my Java code with out passing any arguments. I already tried doing it with

Runtime.getRuntime().exec("src/lexparser.sh");


and

ProcessBuilder pb = new ProcessBuilder("src/lexparser.sh");
Process p = pb.start();


Both of the above methods didn't work. Is there any other method to run a .sh file form Java?

Answer
ProcessBuilder pb = new ProcessBuilder("src/lexparser.sh", "myArg1", "myArg2");
 Process p = pb.start();
 BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
 String line = null;
 while ((line = reader.readLine()) != null)
 {
    System.out.println(line);
 }