javasundaram javasundaram - 1 month ago 8
Java Question

How to call shellscript in java program?

How to call shellscript in java program?

String sCommandString="sh /home/admin/ping.sh";
Process p = Runtime.getRuntime().exec(sCommandString);

Answer

simplest way to run command and get ouput:

Process p = Runtime.getRuntime().exec("ls");
p.waitFor();
Scanner s = new Scanner(p.getInputStream());
while (s.hasNextLine()) {
    String l = s.nextLine();
    System.out.println(l);
}