Problem with BufferedReader readline() method
807603Sep 17 2007 — edited Oct 21 2007Hello,
I am currently exploring the Process class of the API, and have written a simple method which runs the windows command console (cmd.exe) and executes a native Windows program (such as 'netstat' or 'dir' ), and obtains the output.
I am using a BufferedReader's readLine() method in a while loop to read the output of the Process (cmd.exe), and I noticed that the program seems to hang. During debugging, I noticed that the last call to readLine() inside the loop does not return (complete).
I would be grateful if someone could point out the reason for this behaviour.
Thanks!
Source:
static void process ()
{
try
{
Process cmdLine = Runtime.getRuntime().exec ("cmd");
BufferedReader br = new BufferedReader (new InputStreamReader (cmdLine.getInputStream()));
BufferedWriter bw = new BufferedWriter (new OutputStreamWriter(cmdLine.getOutputStream()));
bw.write("netstat\n");
bw.flush();
String line = br.readLine();
while (line != null)
{
System.out.println(line);
line = br.readLine();
}
}
catch (IOException ex)
{
ex.printStackTrace();
System.exit(1);
}
}
Edited by: Kunid on Sep 17, 2007 12:22 PM