Java “constant string too long” compile error. Only happens using Ant, not when using Eclipse

前端 未结 11 2139
抹茶落季
抹茶落季 2020-12-01 15:25

I have a few really long strings in one class for initializing user information. When I compile in Eclipse, I don\'t get any errors or warnings, and the resulting .jar runs

11条回答
  •  天命终不由人
    2020-12-01 16:16

    A workaround is to chunk your string using new String() (yikes) or StringBuilder, e.g.

    String CONSTANT = new String("first chunk") 
                    + new String("second chunk") + ... 
                    + new String("...");
    

    Or

    String CONSTANT = new StringBuilder("first chunk")
                    .append("second chunk")
                    .append("...")
                    .toString();
    

    These can be viable options if you're producing this string e.g. from a code generator. The workaround documented in this answer where string literals are concatenated no longer works with Java 11

提交回复
热议问题