JDK-6347283 : Console.writer/readLine/rePassword does not work after closing the System.out/in
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6
  • Priority: P4
  • Status: Resolved
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2005-11-08
  • Updated: 2013-09-15
  • Resolved: 2013-09-15
Related Reports
Relates :  
Description
The PrintWriter Stream associated with Console.writer() closes/doesn't work whenever we close stream OutputStreamWriter(System.out).The follwing program displays nothing on the console.After flushing the stream con.flush(), "Hi Duke" is not displaying on the console.
It has not been mentioned in the spec that, closing the other output streams will affect the Console methods Console.format(String fmt,Object... object),printf(),readLine(String fmt,Object... object).
Please see the following version,code and Result.
<version>
C:\work>java -version
java version "1.6.0-auto"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-auto-309)
Java HotSpot(TM) Client VM (build 1.6.0-beta-b60, mixed mode)
</version>

<code>
import java.io.*;
class TestClose3{
  public static void main(String ... s1) {
   Console con = System.console();
   try{
     OutputStreamWriter writer = new OutputStreamWriter(System.out);
     writer.close();
     con.printf("hello Java");
     String ln=con.readLine("%s","Enter Line");
     con.printf("line Entered %s",ln);
     char ch[] = new char[20];
     ch=con.readPassword("%s","Enter password");  
     con.format("Password Entered %s",new String(ch));
     Writer riter = con.writer();
     riter.write("Hi Duke",0,6);
     con.flush();
     
    }catch(Exception e){
      e.printStackTrace();
    }

  }
}
</code>
<Result>
(Nothing displayed on the console except those characters typed.)
</Result>
Console's input/output methods no longer work after closing the System.in/out.

Comments
as evaluation suggested it's not a bug
15-09-2013

EVALUATION Copy/Pasted Iris's evaluation from 6347312. ----------------------------------------------- You say: Closing InputStreamReader(System.in) should not affect Console readLine(), readPassword() methods. I disagree. The spec for Console says that invoking close() on the objects returned by reader() will not close the underlying stream. That's not how you've aquired the reader. In your test case, the spec for Reader.close() (which is inherited by InputStreamReader.close()) is applicable: Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect. There is exactly one System.in for every VM, so it should not be surprising that any operations directed to it would fail after invoking close() in this manner. As for the specific detail message, I believe that we're simply propagating the message from the underlying OS. It may be possible to provide a slightly better error message if we could detect that the console device has been closed before we actually try to read it. However, this may not be possible since Reader does not provide any mechanism to detect whether it has been previously closed. Passing this bug along to the engineer responsible for Console to see if he has an thoughts on how we might provide a better detail message. *** (#1 of 2): 2005-11-11 11:26:15 PST ###@###.### ------------------------------------------------------------
08-12-2005