0

Referring to

How to add a timeout value when using Java's Runtime.exec()?

But I am always getting worker.exit value NULL so it always throws timeout exception. Below is my code

public class MyClass {


    private static class Worker extends Thread {
        private final Process process;
        private Integer exit;

        private Worker(Process process) {
            this.process = process;
        }

        public void run() {
            try {
                exit = process.waitFor();
            } catch (InterruptedException ignore) {
                return;
            }
        }
    }


    public String run(String command, long replyTimeout) throws Exception {

        StringBuffer output = new StringBuffer();
        Process p;
        p = Runtime.getRuntime().exec(command);

    BufferedReader errReader = new BufferedReader(new InputStreamReader(
            p.getErrorStream()));

    BufferedReader inputReader = new BufferedReader(new InputStreamReader(
            p.getInputStream()));

        Worker worker = new Worker(p);
        worker.start();

        try {
            worker.join(replyTimeout);              


            if (worker.exit != null) {
                if (worker.exit > 0) {

                    String line = "";
                    while ((line = errReader.readLine()) != null) {
                        output.append(line + "\n");
                    }
                    System.out.println(output.toString());
                    System.out.println(worker.exit);
                    throw new Exception(output.toString());
                } else {

                    String line = "";
                    while ((line = inputReader.readLine()) != null) {
                        output.append(line + "\n");
                    }
                    System.out.println(output.toString());
                    System.out.println(worker.exit);
                    return output.toString();
                }
            } else {
                throw new TimeoutException();
            }

        } catch (InterruptedException ex) {
            worker.interrupt();
            Thread.currentThread().interrupt();
            throw ex;
        } finally {
            p.destroy();
        }

    }



}
Community
  • 1
  • 1
vishal
  • 3,993
  • 14
  • 59
  • 102

1 Answers1

4

You're doing this all wrong. You have to consume all the output of a process, on both stdout and stderr, before it makes any sense to call waitFor(). Otherwise the process can block trying to write its own output.

NullPointerExceptions on the other hand are just due to trivial coding bugs that you're expected to be able to iron out on your own. At least I expect it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • this is how it is mentioned here http://stackoverflow.com/questions/808276/how-to-add-a-timeout-value-when-using-javas-runtime-exec – vishal May 20 '14 at 10:27
  • do you have any idea why exit value is null always ? – vishal May 20 '14 at 10:47
  • 1
    No, but you do. You have the stack trace, you have the line number at which it was thrown, you have the line of code with that line number, you have the variable being dereferenced on that line, and you have its prior history. You don't need anybody else's help. We on the other hand don't have any of that. – user207421 Jun 06 '14 at 22:09