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

execing a program and reading it's output hangs

XMLWordPrintable

    • 1.0fcs
    • sparc
    • solaris_2.4
    • Not verified

      Execing a program and reading it's output sometimes hangs in the read.
      I think could work around it if there was some equivalent to select(), or a way to
      attach a callback to a signal, or a way to tell if a process has died. If there
      are ways to do these things, please let me know.

      Here is a sample program.

      import java.io.*;
      //
      // Cat the output of a file. To run: java cat filename
      //
      public class cat {
          public cat(String file) {
              String args[] = new String[2];
              Process p;
              byte buf[] = new byte[100];
              InputStream s;
              int length;

              args[0] = "/bin/cat";
              args[1] = file;
              try {
                  p = Runtime.getRuntime().exec(args);
                  s = p.getInputStream();

                  System.out.println("Before read");
                  while ((length = s.read(buf)) != -1) {
                      System.out.println("After read");
                      System.out.println(new String(buf,0,0,length));
                  }
              } catch (IOException e) {
                  System.out.println("command failed.");
              }
              System.out.println("got to here");
          }

          public static void main(String args[]) {
              if (args.length == 0) {
                  System.out.println("enter file");
              } else {
                  new cat(args[0]);
              }
          }
      }

      Running it giving it a file that exists works ok, but hangs if the file does not exit.
      I tried many, many different tests which included execing a shell script which
      would hang no matter what! So I'm not sure what it is related to. I thought it
      might be the nonzero exit status, but the shell script exited 0 and the read hung.

      By the way, the above is a test case. The program I really want to exec is javac
      and read the compile errors. If there are errors, the read always hangs.

            duke J. Duke
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: