Running a bash shell script in java

You should use the returned Process to get the result.

Runtime#exec executes the command as a separate process and returns an object of type Process. You should call Process#waitFor so that your program waits until the new process finishes. Then, you can invoke Process.html#getOutputStream() on the returned Process object to inspect the output of the executed command.

An alternative way of creating a process is to use ProcessBuilder.

Process p = new ProcessBuilder("myCommand", "myArg").start();

With a ProcessBuilder, you list the arguments of the command as separate arguments.

See Difference between ProcessBuilder and Runtime.exec() and ProcessBuilder vs Runtime.exec() to learn more about the differences between Runtime#exec and ProcessBuilder#start.

Leave a Comment