Skip to content

Instantly share code, notes, and snippets.

@nicksieger
Created October 21, 2011 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicksieger/1304893 to your computer and use it in GitHub Desktop.
Save nicksieger/1304893 to your computer and use it in GitHub Desktop.
open "yes" and try to wait for it to finish
// According to http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/yes.c,
// yes is supposed to exit when writing to its stdout stream returns EOF. So
// why does this program hang on #waitFor?
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class POpen {
public static void main(String[] args) {
try {
Process p = Runtime.getRuntime().exec("yes");
InputStream s = p.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(s));
for (int i = 0; i < 5; i++) {
System.out.println(r.readLine());
}
s.close();
p.getErrorStream().close();
p.getOutputStream().close();
int result = p.waitFor(); // hangs here, why?
System.out.println("exit: " + result);
System.exit(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment