0

Im trying to run a program that takes a long time to finish inside a java program. The program inside the java program outputs a huge file (somewhere between 4 to 6 GB). I use the following code inside the main method.

//get the runtime goinog
Runtime rt = Runtime.getRuntime();
//execute program
Process pr = rt.exec("theProgram.exe");
//wqit forprogram to finish
pr.waitFor();

I get a number of errors:

  • when the java program ends theProgram.exe does not stop sometimes the
  • java program never ends even when theProgram.exe has ended
  • theProgram.exe stops without finishing, and the java program does not stop.

More information:

  • I'm using cygwin in Windows7
3
  • Please explain what you mean by the theProgram.exe stops without finishing Commented Dec 5, 2011 at 18:10
  • I mean, I know the expected result because I have run it directly on the command line, but when i run it inside the java program sometimes it stops, and I dont see the same result. In these cases, the java program does not stop running Commented Dec 5, 2011 at 18:33
  • If the process has useful output to gauge its progress, try redirecting its output as I demonstrate in my answer. Commented Dec 5, 2011 at 18:37

2 Answers 2

1

Calling this method with your Process pr will terminate the process when your java program exits:

private void attachShutdownHook(final Process process) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            process.destroy();
        }
    });
}

If your process has output you can use to assess its progress, then redirect output to java by calling:

private void redirectOutputStreamsToConsole(Process process) {
    redirectStream(process.getInputStream(), System.out);
    redirectStream(process.getErrorStream(), System.err);
}

private void redirectStream(final InputStream in, final PrintStream out) {
    new Thread() {
        @Override
        public void run() {
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String line = null;
                while ((line = reader.readLine()) != null)
                    out.println(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }.start();
}
Sign up to request clarification or add additional context in comments.

4 Comments

could I just add pr.destroy() at the end of the program?
This is preferable because most programs can have multiple exit points, for instance if it unexpectedly throws an Exception you will probably want to make sure the process terminates.
this worked great, now what should I change if I did not want to show the progress output. thanks
Simply don't call the redirectOutputStreamsToConsole method or you could redirect to a different PrintStream such as a log file.
1

It would be a good idea to include pr.destroy() at the end of your Java code so that it terminates the process when your program has ended. This solves error #1

What does pr.exitValue() return in these cases?

1 Comment

i will include pr.destroy() and i will do a run with pr.exitValue() and get back to you. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.