File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Exiting a Runtime.exec() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exiting a Runtime.exec()" Watch "Exiting a Runtime.exec()" New topic

Exiting a Runtime.exec()

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I am running the following code. It does all my dos command fine but it won't 'finish' for me. I'd like it to get to a 'Press Return to Continue' message or something like that. Instead, it just hangs. As you can see, I've added close() and flush() and destroy() methods in the hope that one of them will do this for me but no luck yet.

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Your program is in the br.readLine() method, waiting for the cmd program to output another line. It doesn't, because you haven't sent it any more commands. But the cmd process has no way of knowing that you won't be sending any more commands, so it sits waiting for more. Meanwhile the BufferedReader (and enclosed streams) is waiting for an EOF, which would tell it to go ahead and let readLine() return a null, nice no more lines will be sent. But that's not happening.
To break this deadlock, the easiest thing to do is to take your dos.close() method and move it up to before the br.readLine() loop. This way, the cmd process knows that there will be no more commands issued to it, and so it can go ahead and shut itself down - which among other things, means it will send an EOF to our BufferedReader, which will allow it to see that there is nothing else to read, and return a null from the readLine() method. This will allow your method to complete normally.

"I'm not back." - Bill Harding, Twister
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Works great. Thanks!
I agree. Here's the link:
subject: Exiting a Runtime.exec()
It's not a secret anymore!