This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Regarding Executing a batch file in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regarding Executing a batch file in Java" Watch "Regarding Executing a batch file in Java" New topic
Author

Regarding Executing a batch file in Java

S. Vijay
Ranch Hand

Joined: Jul 10, 2009
Posts: 44
Hi Everyone

I have a question in the below code. I am trying to run a "server.bat" in a specified location. The batch file runs and waits for multiple clients and it stops when we close it. Now my problem is when I run the below code my server is running perfectly but the program waits for the batch to complete inspite of me not giving waitfor method for the process. My program shouldn't wait for the batch file to complete. It has to run the command in a new window and control the other flow of program which is not done here in this program. Can anyone help me whats the problem is?




Thanks for your help
-Vijay
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You are still waiting for the process to finish, as that's the only moment when the process' output will end. So the JavaWorld article "When Runtime.exec() won't" is still valid here -- read from the error stream as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
As Rob says, you will block your output if you don't empty both streams, but it appears to be the output stream you are forgetting, and the error stream you are reading from.

By the way: reserve System.exit(0) for normal termination; use System.exit(i) where i is a non-zero int for abnormal termination, as you have in your catch block.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding Executing a batch file in Java
 
Similar Threads
Runtime.getRuntime().exec
How to get a value from process
Runtime.exec( ) is hanging....
Wireshark connectivity using java and string compare from text file
Starting a server via threads