I'm having problems with running an exernal application out of Java. I'm aware of the problem with the input and output streams, and I'm trying to read them as follows:
ProcessBuilder pb = new ProcessBuilder(args);
args[args.length - 1] += "<NUL";
pb.redirectErrorStream(true);
Process p = pb.start();
InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader input = new BufferedReader(isr);
while (input.readLine() != null) {
}
p.waitFor();
input.close();
isr.close();
The command I'm executing is xdg-open for an URL, so it shouldn't wait for input.
This works nine times out of ten on my machine, but sometimes it simply hangs at pb.start(); and the command isn't executed.
The java thread is waiting on the process to return. Here's the stack trace:
Name: LinkHandlerExec
State: WAITING on java.lang.UNIXProcess$Gate@356122dc
Total blocked: 0 Total waited: 1
Stack trace:
java.lang.Object.wait(Native Method)
java.lang.Object.wait(Object.java:502)
java.lang.UNIXProcess$Gate.waitForExit(UNIXProcess.java:80)
java.lang.UNIXProcess.<init>(UNIXProcess.java:161)
java.lang.ProcessImpl.start(ProcessImpl.java:81)
java.lang.ProcessBuilder.start(ProcessBuilder.java:468)
Here's what strace tells me:
$ sudo strace -p 13255
Process 13255 attached - interrupt to quit
futex(0x7fbb673e49d0, FUTEX_WAIT, 13262, NULL