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.
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.
- relates to
-
JDK-1228933 exec() only works correctly the first time called
- Closed