0

I want to read a key from console but I get this exception and I am pretty stacked on it. I would appreciated any help!

public char readKey() {
        char key = ' ';
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            key = (char) br.read();
        } catch (IOException e) { 
            e.printStackTrace();
        }
        return key;
    }

java.io.IOException: Stream closed at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162) at java.io.BufferedInputStream.read(BufferedInputStream.java:325) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:154) at java.io.BufferedReader.read(BufferedReader.java:175)

stebetko
  • 735
  • 1
  • 8
  • 24
  • 2
    Seems pretty clear: you are running the program without a console. – Joni Aug 21 '13 at 08:23
  • What do you mean "without a console"? – stebetko Aug 21 '13 at 08:27
  • 1
    The error message says System.in is closed. This means that it's not connected to an open device or file. In other words, the program is running without a console. If you told is where and how you are running the program someone might be able to suggest a fix. – Joni Aug 21 '13 at 08:30
  • I try to run it from eclipse.. – stebetko Aug 21 '13 at 08:31
  • @stebetko: Are you running this from a fairly small body of code in Eclipse? If so, post that additional code. – Andrew Martin Aug 21 '13 at 08:37
  • @AndrewMartin It is a part of more complex logic. I use this to test websocket communication with tomcat server. So when message is received there is an option to choose response through console input. – stebetko Aug 21 '13 at 08:51

0 Answers0