Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4211075

System.setOut(System.out) fails to switch PrintStream back to STD_OUT

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 1.1.6
    • vm-legacy



      Name: dbT83986 Date: 02/11/99


      The following code:

      import java.io.*;

      public class Test {

        public static void main(String[] args) {

           try
           {
              PrintStream errPrintStream = new PrintStream(new FileOutputStream("std.out"));

              System.setOut(errPrintStream);

              for(int i = 0; i < 2; i++)
              {
                 System.out.println("\nHello File World!!!");
              }
              errPrintStream.close();

              System.setOut(System.out);

           }
           catch(Exception e)
           {
              System.out.println("SOMETHING WENT WRONG");
           }

           for(int i = 0; i < 2; i++)
           {
              System.out.println("\nHello Outside World!!!");
           }
        }
      }
      -------
      Sends "Hello File World!!!" to std.out while "Hello Outside World!!!" is never printed at all. Removing ".close()" call results printing
      of both greetings to a file. However, after a seemingly irrelevant change and running this code with "-nojit" option results in
      expected outcome:
      -------
      import java.io.*;

      public class Test {

        public static void main(String[] args) {

           try
           {
              PrintStream outPrint = System.out;

              PrintStream errPrintStream = new PrintStream(new FileOutputStream("std.out"));

              System.setOut(errPrintStream);

              for(int i = 0; i < 2; i++)
              {
                 System.out.println("\nHello File World!!!");
              }
              errPrintStream.close();

              System.setOut(outPrint);

           }
           catch(Exception e)
           {
              System.out.println("SOMETHING WENT WRONG");
           }

           for(int i = 0; i < 2; i++)
           {
              System.out.println("\nHello Outside World!!!");
           }
        }
      }

      -------
      NOTE: Both 1.1.7 and 1.2 produce the same error. Accept I found no way to set JIT off in 1.2.
      -------

      If I missing something I would really appreacite it if you could point it out to me. Otherwise I will assume this is a bug.

      Good day!!!
      (Review ID: 52416)
      ======================================================================

            Unassigned Unassigned
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: