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

Runtime.waitFor() hangs after process exits

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P1
    • Resolution: Fixed
    • Affects Version/s: 1.1
    • Fix Version/s: 1.1
    • Component/s: core-libs
    • Subcomponent:
    • Resolved In Build:
      1.1
    • CPU:
      sparc
    • OS:
      solaris_2.4
    • Verification:
      Not verified

      Description

      VM hangs after executing new Process please check attached program for recreating problem. This works fine in JDK1.1 Alpha build and previous releases.
      I am running latest JDK1.1M.


      To compile use command javac TestExec.java
      and run command as follows:
      java TestExec `which javac`

      And observe that VM never exits. This will cause any program that waits for
      VM to close stderr and stdout streams.


      ------------8<------------TestExec.java -----------------------

      import java.io.*;
      import java.awt.*;

      class DisplayThread extends Thread {

        DataInputStream inStream;
        /**
         * Constructor
         */
        DisplayThread(DataInputStream stream){
          inStream = stream;
        }

        protected void display(String line) {
          System.out.println(line);
        }

        public void run() {
          String Line;
          try {
            while ((Line = inStream.readLine()) != null ) {
      display(Line);
            }
          }
          catch (IOException i) { // Ignore exception
          }
        }
      }


      public class TestExec {

        private DataInputStream stdout,
               stderr;

        private DataOutputStream stdin;

        private Process newProc;

        protected DisplayThread dispStdout,
                                dispStderr;

        TestExec(String[] shellCmds) {
          try {
            // Get new runtime
            newProc = Runtime.getRuntime().exec(shellCmds);

            // Bind stderr First
            stderr = new DataInputStream(
      new BufferedInputStream(newProc.getErrorStream(),1024));
            // Bind stdout
            stdout = new DataInputStream(
      new BufferedInputStream(newProc.getInputStream(),1024));
            // Bind stdin
            stdin = new DataOutputStream(
      new BufferedOutputStream(newProc.getOutputStream()));

            String Line;
            
            // Start Display Thread for Printing Messages in Background

            displayStdout(stdout);

            displayStderr(stderr);

            newProc.waitFor();
            System.out.println("Exit Value :" + newProc.exitValue());
          }
          catch (Exception i) {
            System.out.println(i);
          }
        }

        // This is interface that subclass can modify for stdout and stderr
        protected void displayStdout(DataInputStream stdout) {
          dispStdout = new DisplayThread(stdout);
          dispStdout.start();
        }

        // Display stdError
        protected void displayStderr(DataInputStream stderr) {
          dispStderr = new DisplayThread(stderr);
          dispStderr.start();
        }
        
        public static void main(String[] args) {
          System.out.println("Executing command :" + args[0]);
          TestExec t = new TestExec(args);
          System.out.println("Completed Successfully");
        }
      }
          
       

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                djbrown David Brown (Inactive)
                Reporter:
                duke J. Duke (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: