stdout and stderr don't occur sequentially in Eclipse and Netbeans

Z

ZelluX

Here's a quite simple program which is expected to print 1, 2, 3 in
order
public static void main(String[] args) {
System.err.println("1");

System.out.println("2");
System.out.flush();

System.err.println("3");
}

But it will fail when running on Eclipse or Netbeans

How to make it print sequencially?

Many thanks
 
G

GArlington

Here's a quite simple program which is expected to print 1, 2, 3 in
order
public static void main(String[] args) {
You are writing to two differnt streams and flushing only one of them,
what do you expect?
 
G

GArlington

Here's a quite simple program which is expected to print 1, 2, 3 in
order
public static void main(String[] args) {
System.err.println("1");

System.out.println("2");
System.out.flush();

System.err.println("3");
}

But it will fail when running on Eclipse or Netbeans

How to make it print sequencially?
Flush after every write.
 
Z

ZelluX

order
    public static void main(String[] args) {

You are writing to two differnt streams and flushing only one of them,
what do you expect?
        System.err.println("1");
        System.out.println("2");
        System.out.flush();
        System.err.println("3");
    }
But it will fail when running on Eclipse or Netbeans
How to make it print sequencially?
Many thanks

But System.err is unbuffered, so I don't think it will have any effect
to flush after every write.
 
Z

ZelluX

There is nothing in the Javadocs to indicate that System.err is unbuffered..
Like System.out, System.err is a java.io.PrintStream.  The docs for
PrintStream imply that instances are buffered, else the flush() method would
be meaningless.  So it is not accurate, or at least not safe to say that
System.err is unbuffered.

That leaves auto-flushing.  Nothing in the Javadocs for System.err indicates
that it's created with autoFlush on.  Did you check the source for your
particular flavor of Java to see if it is?

In my installation, the source shows:
  setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));

Whaddaya know?  System.err is buffered in my installation.

Sun Java JDK 1.6.0_10b25, 64 bit, Linux.

Are you quite certain that System.err is unbuffered in your Java installation?

I'm not that certain, but I've tried placing a System.err.flush()
after the output, and it doesn't work.
Under consoles like Windows CMD or Linux bash, the problem won't occur.
 
O

Owen Jacobson

Here's a quite simple program which is expected to print 1, 2, 3 in
order
    public static void main(String[] args) {
        System.err.println("1");

        System.out.println("2");
        System.out.flush();

        System.err.println("3");
    }

But it will fail when running on Eclipse or Netbeans

How to make it print sequencially?

Many thanks

Repeat after me:

There are _no_ sequencing promises across multiple streams.

Data written to a single stream will always arrive in the same order
it was written; data written to multiple streams may arrive in any
order at all. All of the following would be valid outputs:
---
1
2
3
 
Z

ZelluX

Repeat after me:

   There are _no_ sequencing promises across multiple streams.

Data written to a single stream will always arrive in the same order
it was written; data written to multiple streams may arrive in any
order at all.  All of the following would be valid outputs:
---
1
2
3

Thanks for you reply.

In linux there's a function setvbuf which can make stdout and stderr
line-buffered (_IOLBF mode), and thus the similar problem in C
programming can be resolved.

Is there anything like setvbuf in Java?
 
A

Andrew Thompson

Is there anything like setvbuf in Java?

What is it you are actually trying to achieve?

The references to System.out and System.err and
the importance of the sequence make me wonder if
using logging is a better option for ..achieving
whatever it is that this app. is supposed to
achieve.
 
P

Patricia Shanahan

ZelluX said:
Here's a quite simple program which is expected to print 1, 2, 3 in
order
public static void main(String[] args) {
System.err.println("1");

System.out.println("2");
System.out.flush();

System.err.println("3");
}

But it will fail when running on Eclipse or Netbeans

How to make it print sequencially?

The simplest way to make two streams behave like a single stream is to
make them a single stream. Try putting

System.setOut(System.err);

or

System.setErr(System.out);

at the start of your main method.

Patricia
 
Z

ZelluX

What is it you are actually trying to achieve?

The references to System.out and System.err and
the importance of the sequence make me wonder if
using logging is a better option for ..achieving
whatever it is that this app. is supposed to
achieve.


My problem is, in the situatations which both stdout and stderr are to
be printed on the console, is it possible to make them occur in the
order that they're called?

I just found this odd behaviour in some IDEs and are curious to know
whether there are any APIs with function like setvbuf ;-)
 
R

Roedy Green

But it will fail when running on Eclipse or Netbeans

How to make it print sequencially?

You want it so save up all the out, and print it after all the err? or
do you want them perfectly interspersed, char by char as they AR
written?

You can output to a file then display the file.

Or you can merge the outputs with set Err.

see http://mindprod.com/jgloss/console.html for details.
 
T

Tom Anderson

ZelluX said:
Here's a quite simple program which is expected to print 1, 2, 3 in
order
public static void main(String[] args) {
System.err.println("1");
System.out.println("2");
System.out.flush();
System.err.println("3");
}

But it will fail when running on Eclipse or Netbeans

How to make it print sequencially?

The simplest way to make two streams behave like a single stream is to
make them a single stream. Try putting

System.setOut(System.err);

or

System.setErr(System.out);

at the start of your main method.

+1

tom
 
L

Lew

Wayne said:
Most likely there is extra buffering going on Eclipse and/or
Netbeans.  I doubt using C's setvbuf would work on these
platforms (try it!).  Perhaps you need to report the buffering
of these streams as a bug in the IDEs, or make a RFE.

On what do you base that "most likely" evaluation?

There's sure plenty of buffering of these streams in the JVM. I see
no need to blame the IDEs.

Even if they do add more buffering, why would that be considered a
bug?

Anyhow, even in the unlikely (!) event that the IDEs are mangling the
buffering of System.out and System.err, having the IDE makers stop
doing that would not eliminate buffering from those PrintStreams, nor
eliminate the BufferedOutputStream that Sun's JVM, for example, puts
atop those.
Unlike with C/C++, Java doesn't mandate that stderr (or any stream)
auto-flush after every write.  You can create a stream with
line flushing (a flush occurs after writing a newline), but the
docs don't state that System.out or System.err ever autoflush.

And the OP has provided us evidence that, in fact, they do not.
 
A

Andrea Francia

ZelluX said:
I'm not that certain, but I've tried placing a System.err.flush()
after the output, and it doesn't work.
Under consoles like Windows CMD or Linux bash, the problem won't occur.

The way that cmd or linux bash deal with the std out end the std err of
a child process may be different from the way that netbeans (or eclipse)
deal with them.
 
A

Andrea Francia

ZelluX said:
My problem is, in the situatations which both stdout and stderr are to
be printed on the console, is it possible to make them occur in the
order that they're called?

I just found this odd behaviour in some IDEs and are curious to know
whether there are any APIs with function like setvbuf ;-)

Even if the standard error and output steams are flushed in a specific
order you don't have no promise on what order the reading process (the
IDE or the shell) decide to output them.

To know this you should read the specification (if there are any) of the
reading process.
 
A

Arne Vajhøj

ZelluX said:
In linux there's a function setvbuf which can make stdout and stderr
line-buffered (_IOLBF mode), and thus the similar problem in C
programming can be resolved.

Is there anything like setvbuf in Java?

Not really.

I think the keyword in what you wrote is "in Linux" - Java is
platform independent and it could be a difficult problem to
implement such functionality on all platforms.

You van still use Patricias workaround.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top