Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

When we try to use a variable that is too long for the Java compiler (larger than 64 KB) we receive a “constant string too long” error from the compiler.

In this tutorial, we're going to show how to solve that error.

2. Describing the Problem

Let's reproduce the problem by writing a small test where we have declared a String that is too long:

public void whenDeclaringTooLongString_thenCompilationError() {
    String stringTooLong = "stringstringstring ... 100,000 characters ... string";  

The String contained in our stringTooLong variable contains text with more than 100,000 characters. A string with these characteristics is available in a file available via the GitHub link at the end. To get the error raised, copy its content and replace the stringTooLong‘s value.

Note, if we run this test from some IDEs, we won't receive any error.

The reason is that IDEs are usually more lenient. However, when trying to compile the project (mvn package) or just trying to execute the tests (mvn test) from the command line we will receive the following output:

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.058 s
[INFO] Finished at: 2020-03-14T17:56:34+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile (default-testCompile) on project core-java-strings: Compilation failure
[ERROR] <path to the test class>:[10,32] constant string too long

This is because the length of a string constant in a class file is limited to 2^16 bytes in UTF-8 encoding.

3. Solving the Problem

Once we have the problem reproduced, let's find a way to solve it. The best way is to store our string in a separate file instead of in a declared variable or constant.

Let's create a text file to store the content of our variable and modify our test to get the value from the file:

public void whenStoringInFileTooLongString_thenNoCompilationError() throws IOException {
    FileInputStream fis = new FileInputStream("src/test/resources/stringtoolong.txt");
    String stringTooLong = IOUtils.toString(fis, "UTF-8");

Another way to solve this problem is to store the content of our variable in a properties file and then access it from our test method:

public void whenStoringInPropertiesString_thenNoCompilationError() throws IOException {
    try (InputStream input = new FileInputStream("src/main/resources/config.properties")) {         
        Properties prop = new Properties();
        String sValue = prop.getProperty("stringtoolong");

Now if we try to compile our project or execute the tests, everything will work:

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.433 s
[INFO] Finished at: 2020-03-14T18:23:54+01:00
[INFO] ------------------------------------------------------------------------

Of course, we could also introduce concatenation with our string, but such isn't recommended. If we have such a long string, our Java files are likely not the best home for it anyway.

4. Conclusion

In this article, we looked at the “constant string too long” compile error. We saw that we can work around it by storing the value of the Strings in separate files or configuration properties.

As always, you can find the code over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!