waiting for the end of the process
843798Sep 8 2003 — edited Sep 10 2003
I'm executing a command using the Runtime class and I have to wait until the end of the command to get the results. But, I don't want to sleep the thread for some undeterminated time. I'd like it just to wait until the command ends.
See in this code:
try
{
String result="";
Process p = Runtime.getRuntime().exec("sh"); //open a new shell
InputStream is = p.getInputStream();
OutputStream os = p.getOutputStream();
os.write("ifconfig -a | grep PROMISC -c\n".getBytes());
os.flush();
try {
Thread.currentThread().sleep(1000);
/*command should take some time to execute so wait for 10 secs .... I don't like this .... I would prefer waiting for the end the command ifconfig*/
}
catch (InterruptedException eIE) { }
while (is.available() > 0)
{
result = result + ( (char) is.read());
}
System.out.println(result);
}
catch (IOException e) {
System.out.println(e);
}