runtime.exec sending EOF immediately to input?

Go To


This is my code to start a process in Windows via java (and gobble the output).

    public static void main(String[] args) throws Exception {
    String[] command = new String[3];
    command[0] = "cmd";
    command[1] = "/C";
    command[2] = "test.exe";
    final Process child = Runtime.getRuntime().exec(command);
    new StreamGobbler(child.getInputStream(), "out").start();
    new StreamGobbler(child.getErrorStream(), "err").start();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(

private static class StreamGobbler extends Thread {
    private final InputStream inputStream;
    private final String name;

    public StreamGobbler(InputStream inputStream, String name) {
        this.inputStream = inputStream; = name;

    public void run() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(
            for (String s = in.readLine(); s != null; s = in.readLine()) {
                System.out.println(name + ": " + s);
        } catch (Exception e) {

Somehow the program in question (process) is recieving an EOF right away (as in right after I step pas the "exec" line) and thus throwing an error ( detected, invalid) message immediately after runtime.exec is called. I can run this program manually via command prompt without this issue, but have confirmed that sending a ctrl-z on windows is what causes this message.

Anyone know what could be causing this?

If it matters, I have tried running the process directly as "test.exe" instead of cmd /c test.exe, but when I do that I can't see the output via the inputStream. And when I do cmd test.exe without the /c, there is no difference.

2012-04-04 01:45
by user1309154


Your code looks like it should work (with one caveat, see below).

I took your code verbatim and replaced test.ext with sort, which can read from piped stdin.

If I run the code as-is, it starts the sort command, which waits for input. It hangs at child.waitFor() because you don't close the output stream to indicate EOF. When I add the close() call, everything works correctly.

I suggest you look at test.exe and determine if it is capable of reading from piped stdin, or is expecting console input.

2012-04-04 03:48
by Jim Garrison
Thank you Jim, that's precisely what I needed. It does look like the program doesn't handle stdin the way sort or other cmd line utilities do - user1309154 2012-04-04 13:26


Get rid of "cmd" and "/c". At present you are feeding output to cmd.exe, not to test.exe.

2012-04-04 02:50
by user207421
The problem is that when I do what you say, somehow I am not receiving any stdout from the program via the InputStream the way I do now, as I mentioned at the end of the post. Do you know why that might be the case? I am using cmd /c because it works with every other command line tool except this specific one - user1309154 2012-04-04 03:06
@user1309154 See Jim Garrison's answer. You must definitely close the output stream before you call waitFor() - user207421 2012-04-04 09:18