0

I'm confused with the console output in these little pieces of code:

public static void main(String[] args) throws Exception {
    String path = "/hi/my/path";
    System.out.println(path);
    System.err.println("ERROR!!!");
}

I would expect output in a linear way:

/hi/my/path
ERROR!!!

But I'm getting:

enter image description here

I also tried with flush() as in the possible duplicate:

public static void main(String[] args) throws Exception {
    String path = "/hi/my/path";
    System.out.println(path);
    System.out.flush();
    System.err.println("ERROR!!!");
    System.err.flush();
}

But that gets:

enter image description here

And if I run:

public static void main(String[] args) throws Exception {
    String path = "/hi/my/path";
    System.out.println(path);
    System.err.println(path);
    System.out.println("ERROR!!!");
    System.err.println("ERROR!!!");
}

I get:

enter image description here

And even stranger, if I execute this:

public static void main(String[] args) throws Exception {
    String path = "/hi/my/path";
    System.out.println(path);
    System.out.flush();
    System.err.println(path);
    System.err.flush();
    System.out.println("ERROR");
    System.out.flush();
    System.err.println("ERROR!!!");
    System.err.flush();
}

I get:

enter image description here

Is there something I'm missing? When I debug it, everything goes in a linear way and the output is the expected output, but not when I run the program normally.

Is there a higher priority for the err output stream relative to out? Is System.err faster than System.out for something?

Looking at the System source code, err and out are both PrintStream.

Any idea why is this happening?

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • I think the bottom line is: don't expect synchronization of those outputs, and that's it – njzk2 Jul 06 '15 at 14:47
  • 1
    [@MarounMaroun](http://stackoverflow.com/users/1735406/maroun-maroun) I would like to know how you can mark as duplicated this question regarding I used the question you proposed inside my question because **answer of the question you posted is totally wrong** as you can read in comments. Also it does not have any explanation or real sollution to the question – Jordi Castilla Jul 06 '15 at 14:48
  • @njzk2 ok, but what if I need to have them synchronized? – Jordi Castilla Jul 06 '15 at 14:49
  • then I would say you're out of luck. I couldn't find anything that suggests that there is any way or reason to act on the printing schedule of those 2. – njzk2 Jul 06 '15 at 14:55
  • either me... that's why I can't understand how @MarounMaroun have closed as duplicate.... :( – Jordi Castilla Jul 06 '15 at 14:57
  • the answer may be incorrect, the questions are still the same. – njzk2 Jul 06 '15 at 15:06
  • as for source code, are you actually using openjdk 6? – njzk2 Jul 06 '15 at 15:07
  • one thing you could try is use `setOut` and setErr` to use a custom printstream and make them synchronize somehow. Possibly simply the same instance of PrintStream for both, if that works for you? – njzk2 Jul 06 '15 at 15:10
  • @njzk2 yes, this example is `Java6` but I tested also in `Java7`` platforms I don't have a consistent behaviour either... About syncronizing `PrintStream`, I'm quite sure it will work. But I wanted to go deeper in the origin of the problem because this can be a real problem in some apps. – Jordi Castilla Jul 07 '15 at 08:06
  • Why is this question getting this many re-open votes? It's an exact duplicate of the linked question. – Cerbrus Jul 07 '15 at 08:32
  • @Cerbrus if you think a question with a false answer is a valid dupe ok, but I don't agree with you... sorry – Jordi Castilla Jul 07 '15 at 08:33
  • The linked question has an answer that explains _why_ ___and___ a answer that provides a _workaround_. You won't get anything better than that by re-opening this _exact duplicate_. Do you seriously disagree with me that the _question_ is an _exact duplicate_? – Cerbrus Jul 07 '15 at 08:35
  • How does that _not_ explain what's happening? Please refrain from using caps and "swearing", keep it professional. – Cerbrus Jul 07 '15 at 08:37
  • 2
    Just because the **answer** you're looking for isn't in the answers on the other question it doesn't mean that your questions aren't the same. You'll just have to be patient and how someone writes an answer to it that fits your needs or keep on researching on your own and post the answer yourself when you find it. – Gemtastic Jul 07 '15 at 08:54
  • @Gemtastic problem is not the **answer** i'm looking for... I don't have any preferences about how to solve or explain the issue.... the problem is than **answer accepted is false** – Jordi Castilla Jul 07 '15 at 10:41
  • @Cerbrus *Do you seriously disagree with me that the question is an exact duplicate?* yes, I do and at least 3 more people do... also there are 6 people that think accepted answer is not a good answer... so... yes... I totally disagree... – Jordi Castilla Jul 07 '15 at 10:43
  • The accepted answer doesn't have to be the one you're looking for, other answers might. The only difference between your question and the other question is a for loop, which really doesn't matter if you know what's causing this "issue" (it's actually a feature not working to your advantage here) to begin with. – Gemtastic Jul 07 '15 at 10:54
  • There are 29 people that think the accepted answer is a good answer. Last I checked, 29 is nearly 5 times as much as 6. It took 5 close-votes to close this as a duplicate, that's also more than the 3 + yours that it has not to re-open. The answer not being to your liking has _nothing_ to do with whether or not the _question_ is a duplicate. Both questions ask _exactly_ the same thing. – Cerbrus Jul 07 '15 at 11:19
  • And I proved in my question the answer is false... Test it if you dont believe.... What If nearly 30 people jump from a building?? you will accept they fly? – Jordi Castilla Jul 07 '15 at 11:25
  • Like I said: Whether or not the answer is correct _doesn't matter_. The _question_ is a duplicate. – Cerbrus Jul 07 '15 at 11:28
  • nope, the question may look same but in my question i proved answer of duplicated as false... so... it's not a duplicated question.... – Jordi Castilla Jul 07 '15 at 11:33
  • @JordiCastilla also, if you look a little further in the duplicate question, there are alternative solutions offered – njzk2 Jul 07 '15 at 12:43
  • @njzk2 I know, its already commented... – Jordi Castilla Jul 07 '15 at 12:47
  • regarding your building jumping thing: https://xkcd.com/1170/ Otherwise, and this is my last comment on the matter: your question and the duplicate question are the same. The answers (not only the first one) give elements of answers. There are indeed comments against the accepted answer, you can upvote them, and add you own. However, I think it's a non-issue. If you are depending on `out` and `err` being sequenced, you are doing something wrong. – njzk2 Jul 07 '15 at 13:54
  • @njzk2 it's funny how your first comment: *don't expect synchronization of those outputs, and that's it* and your last: *you are doing something wrong.* are totally opposite... ^_^ About bridge... *If you can't explain it simply, you don't understand it well enough. **Albert Einstein***. Cheers. – Jordi Castilla Jul 07 '15 at 14:09

0 Answers0