I had hit with the same problem runtime.exec() hangs when i tried to execute a lengthy batch script in windows. The problem was clearly described in the java docs which i never read
![]()
“Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.”
The solution was to clear of the buffer so the process wont hit a deadlock. I have a small sample which could make you more clear.
public class ProcessHandler extends Thread {
InputStream inpStr;
String strType;public ProcessHandler(InputStream inpStr, String strType) {
this.inpStr = inpStr;
this.strType = strType;
}public void run() {
try {
InputStreamReader inpStrd = new InputStreamReader(inpStr);
BufferedReader buffRd = new BufferedReader(inpStrd);
String line = null;
while((line = buffRd.readLine()) != null) {
System.out.println(strType + ” —> ” + line);
}
buffRd.close();} catch(Exception e) {
System.out.println(e);
}}
public static void main(String args[])throws Exception {/* For windows setting to cmd.exe */
String command[] = {“cmd.exe”,”/c”,cmd};
/* executing the command with environments set. */
Process pro = Runtime.getRuntime().exec(cmd);/* handling the streams so that dead lock situation never occurs. */
ProcessHandler inputStream =
new ProcessHandler(pro.getInputStream(),”INPUT”);
ProcessHandler errorStream =
new ProcessHandler(pro.getErrorStream(),”ERROR”);/* start the stream threads */
inputStream.start();
errorStream.start();}
}
May 27, 2006
November 12th, 2009 at 5:58 am
Hi there;
I guess there’s something missing. isn’t there any “import” before the code?
February 9th, 2010 at 3:28 pm
String command[] = {“cmd.exe”,”/c”,cmd}; // Where did this cmd(variable) came from?!
I was taking care of the inputStream only and behind the scene the programm was using the errorStream to spool all messages.
Thanks for the article
Best Regards
Luciano
October 1st, 2010 at 3:04 am
Thanks for the tip. Makes sense now. Works great.
Guess I should have RTFM also.
July 28th, 2011 at 7:01 am
Thanks a lot for the solution. I was trying oracle exp command using getRuntime it was just not returning. I tried many ways and I increased java heap size also. Nothing helped. Your solution solved the problem. I was reading standard input and output streams without thread.
Thanks
November 3rd, 2011 at 10:24 pm
thx alot for this solution, but is there any possibility to solve this without thread? i found a solution once on the web, but i can’t find it anymore…
November 21st, 2011 at 1:51 am
[...] a bit of googling Duncan found this blog which explained that we needed to process the output stream from our process otherwise it might end [...]
February 7th, 2012 at 12:36 pm
Nice Post , thanks a lot
April 10th, 2012 at 3:51 pm
Thanks for sharing this info…
July 4th, 2012 at 6:50 pm
Thanks for sharing however the solution doesn’t work… same hanging issue is still there
April 17th, 2013 at 12:12 am
Hi it’s me, I am also visiting this web site regularly, this website is truly fastidious and the people are actually sharing fastidious thoughts.