Problem

Why does my call to Runtime.getRuntime().exec("....") hang?

Solution

The Runtime.getRuntime().exec("....") call may hang, as stated in JDK's javadoc, because:


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.

A simple solution is to just read the stream and discard it:

                Process proc = Runtime.getRuntime().exec("myProgram");
                InputStreamReader isr = new InputStreamReader(proc.getInputStream());
                BufferedReader br = new BufferedReader(isr);
                String line=null;
                while ( (line = br.readLine()) != null);

More details on this and other issues of Runtime.getRuntime().exec() in When Runtime.exec() won't.

-- NicolasBarriga - 15 May 2007