Monday, February 21, 2005

How to pass parameters to interactive shell script from java

It is very small but tricky, just try it


public static int executeWithInput(String cmd) throws Exception
{
int retCode = 0;
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
OutputStream out = pr.getOutputStream();
out.write('y');
out.write('\n');
out.flush();
pr.waitFor();
retCode = pr.exitValue();
return retCode;
}

2 comments:

Anonymous said...

this code seems to be not working ...

Archana said...

Whats the issue you are facing ?
Please provide your sample code, for debugging.