user3545586 user3545586 - 1 year ago 80
Java Question

run a .sh file using java

I want to run a .sh file using java. I want a terminal to be opened and then I can execute another commands in the same terminal and finally destroy it.
I already used ProcessBuilder but I could not accomplish this.
My piece of code:

ProcessBuilder pb = new ProcessBuilder("/home/omar/ros_ws/");
Process p = pb.start();

This method used to work in another code, but I don't know why it's not working in mine.

Thanks in advance


How do you know that it doesn't execute? Maybe you just aren't seeing its result. You should get p.getInputStream() after executing and print in your console, like:

BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)

Also if you're using jdk 7+, try:

Process p = pb.start();