Thanks guys for the answers. It was greatly helpful.
I used the following code:
try {
Runtime rt = Runtime.getRuntime();
String[] cmd = {"/bin/bash", "-c", "myscript.sh"};
System.out.println("Command to execute: " + cmd);
Process pr = rt.exec(cmd);
System.out.println("Pausing 10 sec...");
Thread.sleep(10000);
System.out.println("Pausing 10 sec... done");
OutputStream stdin = pr.getOutputStream();
String carriageReturn = "\n";
stdin.write(carriageReturn.getBytes() );
stdin.flush();
stdin.close();
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line=null;
while((line=input.readLine()) != null) {
// System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}