tags:

views:

410

answers:

1

I get a weird error ("The process tried to write to a nonexistent pipe.") if I stop reading from piped input, from a program that works fine for non-piped input. How can I avoid causing this error?

code:

package com.example.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PipeTest {
    static public void main(String[] args) throws IOException
    {
     BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
     int i = 0;
     while (i < 10)
     {
      String s = r.readLine();
      if (s == null)
       break;
      ++i;
      System.out.println(i);
     }
    }
}

runtime output (testfile.txt is just a large text file with more than 10 lines):

C:\proj\java\test-pipe\bin>java com.example.test.PipeTest < ../testfile.txt    
1
2
3
4
5
6
7
8
9
10

C:\proj\java\test-pipe\bin>type ..\testfile.txt | java com.example.test.PipeTest
1
2
3
4
5
6
7
8
9
10
The process tried to write to a nonexistent pipe.
+1  A: 

The error is coming from your command shell, not from the Java program. It's complaining because "type" is still trying to write to its output, but that pipe was abruptly closd when the Java program terminated.

Jonathan Feinberg
so the better way to handle it is...?
Jason S
Handle what? What's the problem? Is it just that the message seems unsightly to you?
Jonathan Feinberg
Error free behaviour sounds like something one should aim for.
Photodeus