Posts Tagged ‘Scripting’

How to: Execute shell script in java code

Friday, April 17th, 2009

I was trying to use both shell scripts and java to do automation of some sort and I got into thinking, what if I insert shell script in my java code?

Here’s an example:

String command = "echo Hello World!!";

ProcessBuilder pb = new ProcessBuilder("bash", "-c", command);
pb.redirectErrorStream(true);
Process shell = pb.start();