This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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. Thanks!
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.