Created
October 21, 2011 20:39
-
-
Save nicksieger/1304893 to your computer and use it in GitHub Desktop.
open "yes" and try to wait for it to finish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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