Why runtime.exec hangs in Java ?


I had hit with the same problem runtime.exec() hangs when I tried to execute a lengthy batch script in windows. The Java docs had apparently described this which I never read. RTFM😉

“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 empty the buffer so that the process won’t hit a deadlock. I have a small sample which could make you clearer.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ProcessHandler extends Thread {
    InputStream inputStream;
    String streamType;

    public ProcessHandler(InputStream inputStream, String streamType) {
        this.inputStream = inputStream;
        this.streamType = streamType;
    }

    public void run() {
        try {
            InputStreamReader inpStrd = new InputStreamReader(inputStream);
            BufferedReader buffRd = new BufferedReader(inpStrd);
            String line = null;
            while ((line = buffRd.readLine()) != null) {
                System.out.println(streamType+ “::” + 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”, args.toString()
        };

        /* executing the command with environments set. */
        Process proc = Runtime.getRuntime().exec(command);
        /* handling the streams so that dead lock situation never occurs. */
        ProcessHandler inputStream = new ProcessHandler(proc.getInputStream(), “INPUT”);
        ProcessHandler errorStream = new ProcessHandler(proc.getErrorStream(), “ERROR”);
        /* start the stream threads */
        inputStream.start();
        errorStream.start();
    }

}

10 thoughts on “Why runtime.exec hangs in Java ?

  1. 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

  2. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s