Question : Process.waitFor() and InputStream

Hi,

I'm executing a Linux OS-level command using Runtime.getRuntime().exec() method.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
public void run()
{
	String linuxCommand = "myLinuxCommand";
	try
	{
		Process p = Runtime.getRuntime().exec(new String[] {"sh", "-c", linuxCommand});

		p.waitFor();

		BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

		String line;
		while ((line = in.readLine()) != null)
			System.out.println(line);
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
}

I'm reading the response returned back by this OS-level command (see line#12-14).  This command sometime may take upto 10-20 seconds to return the result.  I read from Java API, there is a method waitFor() available in Process.  My question now is, where should I have the waitFor() method in my line of code?  Whether it should be placed before reading the response or it should be placed after reading the response, so that the response returned by the command could be read reliably always?

Answer : Process.waitFor() and InputStream

please refer the updated code
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
<?php
$vardemo = "1";
switch ($vardemo)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}
?>
Random Solutions  
 
programming4us programming4us