开发工具分享
  • 首页
  • 计算科学
  • 文化旅游
  • 项目和网站
    • OSSEZ 计算技术
    • USRealEstate 社区
    • 地区文化
    • CWIKI.US
    • BUG.OSSEZ.COM
    • RSS.OSSEZ.COM
CWIKIUS.CN
一个有独立思考和温度的清新站
  1. Home
  2. Computer Science
  3. This article

Java “constant string too long” 编译错误

2022年08月09日 583Browse 0Like 0Comments

这个问题是编译时错误,在 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 编译错误进行了说明,并且提供了解决的方法。

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

测试源代码

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

https://src.ossez.com/cwiki-us-docs/java-tutorials/src/branch/main/core-java-modules/core-java-strings/src/test/java/com/ossez/stringtoolong/StringTooLongUnitTest.java

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

 

https://www.ossez.com/t/java-constant-string-too-long/14048

Tags: None
Last updated:2022年08月09日

HoneyMoose

有温度的人文和独立的思考

Like
< Previous
Next >

Comments

Cancel reply

Archives
  • May 2026
  • April 2026
  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
Categories
  • Computer Science (2,362)
    • Confluence (663)
    • Gradle (12)
  • U.S. (482)
  • 文化旅游 (145)

COPYRIGHT © 2020 CWIKIUS. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS

湘ICP备2020018253号-1