user10212105
user10212105

Reputation:

Print output and an error message to the console

I'm trying to print output and an error message to the console. but sometimes the sequence of output changes, first it prints the error message & then it prints the simple message can anybody help me understand why is it happening as such? the sequence of output changes most of the time. There is no consistency in the printed output. I'm using eclipse IDE & the output I get is as follows.

I have tried to print the following code,

System.out.println("simple message");  
System.err.println("error message");  

the expected result is this:

simple message

error message

but the actual result is this:

error message

simple message

Upvotes: 6

Views: 3027

Answers (7)

Till Brychcy
Till Brychcy

Reputation: 2924

Even if you flush your streams after writing, these streams are read by different threads in Eclipse and if the writes are almost simultaneously, it happens that the thread for reading stderr is executed before the thread for stdout even if the corresponding write to stdout happened first.

Since Java 1.5 there is the ProcessBuilder class and with this it could be solved in Eclipse by redirecting stderr to stdout during launching - but Eclipse's feature to show stderr output in a different color would be broken by this.

You may add your opinion to https://bugs.eclipse.org/bugs/show_bug.cgi?id=32205

Upvotes: 2

a lovely person
a lovely person

Reputation: 13

as Simeon Ikudabo said the task is impossiple !! but for most close try this

static void print(PrintStream p, Object o) throws InterruptedException {
    Object lock = new Object();
    synchronized (lock) {

        Runnable r = new Runnable() {
            public void run() {
                synchronized (lock)
                {
                p.println(o);
                lock.notify();
                }
            }
        };
        new Thread(r).start();

        lock.wait();
    }

}

public static void main(String[] args) throws InterruptedException {

    for (int i = 0; i < 50; i++) {
        print(System.out, "out");
        print(System.err, "err");

    }}

you might see it as perfect but stil thier is a tiny teeny littel chance of getting out the race and it's not grantted too.

Upvotes: 0

Arpit Shah
Arpit Shah

Reputation: 75

  • Remember, This is not only dependent on the user defined sequence, It is also dependent on the receiving application.
  • For example, If Eclipse IDE has two threads, one for receiving error stream and one for receiving system.out stream then those threads are not in your control and thus you may see sequence changing time to time.
  • If you are not concerned about performance then you can put some sleep in between.

System.out.println("Out");

Thread.sleep(50);

System.err.println("error");

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58782

As suggested in previous answers you can set same stream for both output and error stream using System.setErr

System.setErr(System.out);

Reassigns the "standard" error output stream.

But this setup isn't recommended for real application in production environment

Upvotes: 0

venkatesh bathina
venkatesh bathina

Reputation: 9

try this out ..

System.out.println("Out");

 System.out.flush();

 System.err.flush();

 System.err.println(" err ");

Upvotes: 0

Indunil Aravinda
Indunil Aravinda

Reputation: 813

Since they are two different executing streams, they execute independently. And also they aren't in different block scope so the close() is not called causing not to call flush() as well. Then they are at same level. So JVM takes the lead here and execute those two streams the way he wanted.

Therefore intentionally calling flush() at the end of each stream will help you to get back to the driving seat of your code

Upvotes: 0

Simeon Ikudabo
Simeon Ikudabo

Reputation: 2190

System.out.println() and System.err.println() are different Streams of execution. Output Streams are cached so all the write goes into this memory buffer. After a period of quiet, they are actually written out. Here is a for loop that essentially shows your error again:

for(int x = 0; x <= 5; x++) {
    System.out.println("Out");
    System.err.println("Err");



}

In order to "flush" the Streams call .flush() each time through the loop:

for(int x = 0; x <= 5; x++) {
    System.out.println("Out");
    System.out.flush();
    System.err.println("Err");
    System.err.flush();


}

In this for loop, the out message, and the err message will initially print, but on each flush your out messages would print first, and then your err messages. The output will be something such as:

OutErr
Out
Out
Out
Out
Out

Err
Err
Err
Err
Err

And that is because System.out and System.err are executing on different Streams.

Upvotes: 3

Related Questions