Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with BufferedReader readline() method

807603Sep 17 2007 — edited Oct 21 2007
Hello,

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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 18 2007
Added on Sep 17 2007
6 comments
5,340 views