Java “constant string too long” 编译错误

这个问题是编译时错误,在 Java 的运行时没有这个限制。

当我们在 Java 编译器中输入的变量值超过 64 KB 的话,Java 编译器是不会让编译通过的,你将会得到一个 constant string too long” error from the compiler 错误。

在本文中,我们将会对这个问题的原因进行解释和如何解决这个问题。

问题描述

首先让我们在本地的计算机中重复这个问题,在下面的代码中,插入一个超长的字符串。

@Test
public void whenDeclaringTooLongString_thenCompilationError() {
    String stringTooLong = "stringstringstring ... 100,000 characters ... string";  
    assertThat(stringTooLong).isNotEmpty();
}
复制代码

2022-08-06_09-30-15

上面输入的字符串超过了 100,000 个字符。因为我们是为了测试使用的,你可以在代码中输入任何很长的字符。

有时候你的 IDE 可能不会提示这个错误,但是我们测试的 IEDA 是没有问题的,这个错误能够完全提示出来。

当然可以使用 maven 来进行编译,相同的代码,如果使用 Maven 进行编译的提示如下:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:06 min
[INFO] Finished at: 2022-08-06T09:34:08-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:testCompile (default-testCompile) on project core-java-strings: Compilation failure
[ERROR] /D:/WorkDir/Repository/GitHub/cwiki-us-docs/java-tutorials/core-java-modules/core-java-strings/src/test/java/com/ossez/stringtoolong/StringTooLongUnitTest.java:[16,32] constant string too long
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :core-java-strings

复制代码

在使用 UTF-8 编码的时候,类文件的字符被限制在 2^16 bytes。

问题解决

因为这个问题是编译时错误,不是运行时问题。

解决这个问题的办法也就非常简单了,如果你需要处理超长的字符串的话,我们可以使用 FileInputStream 的方法来进行处理。

将需要处理的字符串放到文件中就可以了。

然后让你的程序从文件中把数据读出来。

如下面的代码所示:

@Test
public void whenStoringInFileTooLongString_thenNoCompilationError() throws IOException {
    FileInputStream fis = new FileInputStream("src/test/resources/stringtoolong.txt");
    String stringTooLong = IOUtils.toString(fis, "UTF-8");
    assertThat(stringTooLong).isNotEmpty();
}
复制代码

当然你也可以定义成一个属性文件中的值,这样的话,你可以通过读取属性文件方法把这个值取出来。

这种方法和上面一种方法的本质是一样的,就是用文件来替换掉。

@Test
public void whenStoringInPropertiesString_thenNoCompilationError() throws IOException {
    try (InputStream input = new FileInputStream("src/main/resources/config.properties")) {         
        Properties prop = new Properties();
        prop.load(input);
        String sValue = prop.getProperty("stringtoolong");
        assertThat(sValue).isNotEmpty();
    }  
}
复制代码

然后尝试对上面的代码再次进行重新编译,有关这个字符串过长的错误就解决了。

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.433 s
[INFO] Finished at: 2020-03-14T18:23:54+01:00
[INFO] ------------------------------------------------------------------------
复制代码

当然,你也可以使用字符串拼接的方式,将希望处理的字符串进行拼接。

但并不十分推荐使用这种方法来进行处理。

结论

在本文中,我们对

constant string too long

编译错误进行了说明,并且提供了解决的方法。

简单来说就是使用文件来进行替换。

测试源代码

相关的测试源代码,请访问:

src.ossez.com/cwiki-us-do…

您也可以 Fork 代码后提交更新。

www.ossez.com/t/java-cons…

猜你喜欢

转载自juejin.im/post/7128764776133427214