3

I have a String, which is then converted into an JSON-object:

public static final String JSON_STRING = "json string";
JSONObject jsonObject = new JSONObject(JSON_STRING);

But sometimes when the String is too long, I get the error:

UTF8 representation for string * is too long for the constant pool

Are there other ways to get a such long string?

Mark Korzhov
  • 2,109
  • 11
  • 31
  • 63
  • Put the data in a file and read from there instead. It is an issue with your adding a too large string literal in the source code. – folkol Sep 08 '14 at 05:52
  • Create several strings and concatenate them in the static initializer? – Karol S Sep 08 '14 at 09:35

2 Answers2

4

Strings are stored in class file in UTF-8 and the limit is 65535 bytes. String constructed in runtime can be much longer

So you should either split your string constant on multiple string constants and concatenate them before use or read the value from text file.

Community
  • 1
  • 1
ponomandr
  • 1,523
  • 13
  • 21
2

As far as I know the JVM (specifically, the classfile format) puts a limit of 65535 characters.

From the Oracle docs:

The length of field and method names, field and method descriptors, and other constant string values (including those referenced by ConstantValue (§4.7.2) attributes) is limited to 65535 characters by the 16-bit unsigned length item of the CONSTANT_Utf8_info structure (§4.4.7).

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331