Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4227759

(process) Process.exitValue() doesn't throw exception if process isn't finished



    • Bug
    • Resolution: Cannot Reproduce
    • P4
    • None
    • 1.2.0
    • core-libs
    • x86
    • windows_95


      Name: krT82822 Date: 04/07/99

      In order to workaround the bug in Proces.waitfor which hangs if there
      is too much output to the pipe (Bug fix promised but not delivered in JDK 1.2)
      I have been relying on the Process.exitValue() to indicate when the process
      has completed. If you call this method on a process that is still running
      it is supposed to throw IllegalThreadStateException. It doesn't always do
      this. Here's some code to illustrate the bug.

      package bugs.example;

      import java.io.*;
      import java.util.*;

      public class SimpleProcess_Tester {

        public SimpleProcess_Tester() {

        public static void main(String[] args) {
          SimpleProcess_Tester simpleProcess_Tester = new SimpleProcess_Tester();
          simpleProcess_Tester.invokedStandalone = true;
        private boolean invokedStandalone = false;

        private void runTest(){
          for(int i=0;i<10;i++)
            String commandString="command.com /c \\\\julia\\main\\jbuilder3\\java\\bin\\javac";

            String StdError="";
            String StdOut="";

            Process p = Runtime.getRuntime().exec(commandString, null);
            InputStream dis = p.getInputStream();
            InputStream err= p.getErrorStream();
            int c;
            //This next little diddy is a workaround to get the output on StdOut.
            //You cannot use process.waitFor() because it hangs. The pipe fills up and the whole thing craps out.
      //It's annoying but there is a fix promised from Sun, although yet to be delivered.
            boolean processRunning=true;
               while(dis.available()>0 && (c = dis.read()) != -1)

               while(err.available()>0 && (c = err.read()) != -1)
                int exitVal=p.exitValue();
                //If we get this far without an exception the process has finished
                //Now suck up any extra characters that might have been generated by
                //the thread before our call to p.exitValue()
                  while(dis.available()>0 && (c = dis.read()) != -1)
                }catch(Exception exc){

                  while(err.available()>0 && (c = err.read()) != -1)
                }catch(Exception exc){

               }catch (IllegalThreadStateException exc) {

             int exitVal=p.exitValue();
             //Do clean up

            System.out.println("Iteration "+i);
            System.out.println("StdErr = "+StdError);
            System.out.println("StdOut = " +StdOut);
          }catch(Exception exc){

       You will notice that most of the time stdout and stderr are empty.
       If you increase the first thread.sleep() to 2000 milliseconds everything
      comes out as it should, because the process has enough time to run.

       The best "bug fix" would be do have a reliable way to tell when
      the process has completed, and then get all the output from the process
      at one time. But until such a time, we need a fix for Process.exitValue().
      (Review ID: 56617)




            hongzh Hong Zhang
            kryansunw Kevin Ryan (Inactive)
            0 Vote for this issue
            0 Start watching this issue