Character limit for System.out.println() in Java
Asked Answered
U

6

8

Is there any character limit for the output of Java's System.out.println(String x) statement?

When I try to print some XML from a web service call using System.out.println(), only a portion of it is actually printed in the console.

The XML string that I am trying to print is huge.

Why is this happening?

Uncouth answered 18/1, 2012 at 14:54 Comment(2)
can you post a screenshot to show the nature of your output ?Governess
I cannot say thank you enough. Why would Eclipse implement this in such a bad way...Lettielettish
O
20

Are you experiencing this within Eclipse? If yes:

EDIT:

  1. Go to Window > Preferences > Run/Debug > Console
  2. Uncheck "Limit Console Output" (Alternatively you can increase the Console buffer size.)

Source

Ozan answered 18/1, 2012 at 14:58 Comment(2)
Just a friendly tip, link-only answers are generally discouraged on Stack Exchange sites. Answers should be able to stand alone without depending on external resources. The reasons for this are described here, here and at several other posts. Usually I just flag posts like this, and mods then delete them, but since you just now posted it I thought I'd give you a chance to update (and possibly earn some rep!) first.Stylography
I understand - but in this special case the answer is right on top of the linked site. Anyways - it might be handy to have the solution on SO, once the linked site goes down. So - edit done...Ozan
S
4

My guess is that you only see the last part of the String because the console has a limited number of lines it can display.

Consider logging to a file from Java, or redirecting the standard output from the program to a file:

java com.foo.bar.Main > output.log
Shelli answered 18/1, 2012 at 14:57 Comment(2)
+1 - there is no limit on the Java side (e.g. in the output stack).Phosphatize
@Stephen_C Output stack? As has been pointed out, there is a limit imposed for a single call to println, but it's huge.Rockie
R
4

You're limited by the maximum size of a Java String. That's all. This should be the equivalent of length Integer.MAX_VALUE (2147483647), which is the max size of an array, since a String is a char array.

Otherwise, it's the Eclipse console capacity limit, as others have said.

Rockie answered 18/1, 2012 at 15:0 Comment(0)
D
3

If you are using Eclipse, it is because there is a limit on the capacity of the Eclipse output console. See this SO question: How do I increase the capacity of the Eclipse output console?

Domingadomingo answered 18/1, 2012 at 14:59 Comment(0)
M
2

I know that printing very long strings into the Eclipse console results in part or all of the string becoming invisible. You may want to break your xml into chunks. If you are only seeing the tail part of the xml then I'd guess its your console buffer trimming off part of it. @Quaylar posted a link about this.

Motorway answered 18/1, 2012 at 15:0 Comment(0)
S
0

There isn't really an explicit maximum, but the offset in the string is determined by int, so Integer.MaxValue would be one limitation IMO. It also would depend on your available memory.

Your best bet would be to stream the output and write portions at a time to ensure you get it all.

Scarlatti answered 18/1, 2012 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.