简体   繁体   English

Java“常量字符串太长”编译错误。 仅在使用 Ant 时发生,在使用 Eclipse 时不会发生

[英]Java "constant string too long" compile error. Only happens using Ant, not when using Eclipse

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 fine.当我在 Eclipse 中编译时,我没有收到任何错误或警告,并且生成的 .jar 运行良好。

Recently, I decided to create an ant build file to use.最近,我决定创建一个 ant 构建文件来使用。 Whenever I compile the same class with ant, I get the "constant string too long" compile error.每当我用 ant 编译同一个类时,都会出现“常量字符串太长”的编译错误。 I've tried a number of ways to set the java compiler executable in ant to make sure that I'm using the exact same version as in Eclipse.我尝试了多种方法来在 ant 中设置 java 编译器可执行文件,以确保我使用的版本与 Eclipse 中的完全相同。

I'd rather figure out how to get the same successful compile I get in Eclipse in Ant than try to rework the code to dynamically concatenate the strings.我宁愿弄清楚如何在 Ant 中获得与在 Eclipse 中相同的成功编译,而不是尝试重新编写代码以动态连接字符串。

Someone is trying to send you a message :-) In the time you've spend fiddling with compiler versions you could have loaded the data from a text file - which is probably where it belongs.有人试图向您发送消息 :-) 在您花时间摆弄编译器版本的过程中,您可以从文本文件中加载数据 - 这可能是它所属的地方。

Check out:查看:

I found I could use the apache commons lang StringUtils.join( Object[] ) method to solve this.我发现我可以使用 apache commons lang StringUtils.join( Object[] )方法来解决这个问题。

public static final String CONSTANT = org.apache.commons.lang.StringUtils.join( new String[] {
  "This string is long", 
  "really long...", 
  "really, really LONG!!!" 
} );

Nothing of above worked for me.以上没有对我有用。 I have created one text file with name test.txt and read this text file using below code我创建了一个名为 test.txt 的文本文件并使用以下代码读取此文本文件

String content = new String(Files.readAllBytes(Paths.get("test.txt")));

The length of a string constant in a class file is limited to 2^16 bytes in UTF-8 encoding, this should not be dependent on the compiler used.类文件中字符串常量的长度在 UTF-8 编码中限制为 2^16 字节,这不应依赖于所使用的编译器。 Perhaps you are using a different character set in your ant file than in eclipse, so that some characters need more bytes than before.也许您在 ant 文件中使用了与 eclipse 中不同的字符集,因此某些字符需要比以前更多的字节。 Please check the encoding attribute of your javac task.请检查您的javac任务的encoding属性。

A workaround is to chunk your string using new String() (yikes) or StringBuilder , eg一种解决方法是使用new String() (yikes) 或StringBuilder对字符串进行分块,例如

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 eg from a code generator.如果您从代码生成器生成此字符串,这些可能是可行的选择。 The workaround documented in this answer where string literals are concatenated no longer works with Java 11此答案记录的解决方法连接字符串文字时不再适用于 Java 11

  String theString2 = IOUtils.toString(new FileInputStream(new     
  File(rootDir + "/properties/filename.text")), "UTF-8");

Another trick, if I'm determined to put a long string in the source, is to avoid the compiler detecting it as a constant expression.如果我决定在源代码中放入一个长字符串,另一个技巧是避免编译器将其检测为常量表达式。

String dummyVar = "";
String longString = dummyVar +
    "This string is long\n" + 
    "really long...\n" + 
    "really, really LONG!!!";

This worked for a while, but if you keep going too far the next problem is a stack overflow in the compiler.这工作了一段时间,但如果你继续走得太远,下一个问题是编译器中的堆栈溢出。 This describes the same problem and, if you're still determined, how to increase your stack - the problem seems to be the sheer size of the method now. 描述了同样的问题,如果您仍然确定如何增加堆栈 - 现在问题似乎是方法的绝对大小。 Again this wasn't a problem in Eclipse.同样,这在 Eclipse 中不是问题。

Did you try this ?你试过这个吗? Never tried it myself, but here is the relevant section:我自己从未尝试过,但这是相关部分:

Using the ant javac adapter The Eclipse compiler can be used inside an Ant script using the javac adapter.使用 ant javac 适配器 Eclipse 编译器可以使用 javac 适配器在 Ant 脚本中使用。 In order to use the Eclipse compiler, you simply need to define the build.compiler property in your script.为了使用 Eclipse 编译器,您只需在脚本中定义 build.compiler 属性。 Here is a small example.这是一个小例子。

 <?xml version="1.0" encoding="UTF-8"?>
 <project name="compile" default="main" basedir="../.">

<property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>

<property name="root" value="${basedir}/src"/>

<property name="destdir" value="d:/temp/bin" />

<target name="main">
    <javac srcdir="${root}" destdir="${destdir}" debug="on" nowarn="on" extdirs="d:/extdirs" source="1.4">
        <classpath>
          <pathelement location="${basedir}/../org.eclipse.jdt.core/bin"/>
        </classpath>
    </javac>        
</target>
</project>

I would really consider making your classes standards compatible.我真的会考虑使您的课程标准兼容。 I believe the official limit is 65535, and the fact that Eclipse is more lenient is something that could change on you at the most inconvenient of times, and either way constantly having to get the project compiled with Eclipse can really start to limit you in too many ways.我相信官方的限制是 65535,而 Eclipse 更宽松的事实可能会在你最不方便的时候改变,无论哪种方式,不断地用 Eclipse 编译项目都会真正开始限制你很多方法。

将您的字符串添加到 values/strings.xml 中,而不是调用 getResources.getString(R.string.yourstring)

I was able to resolve this issue in a similar way like Lukas Eder.我能够像 Lukas Eder 一样以类似的方式解决这个问题。

String testXML = "REALLY LONG STRING...............................";
       textXML += "SECOND PART OF REALLY LONG STRING..........";
       textXML += "THIRD PART OF REALLY LONG STRING............";

Just split it up and add it together;只需将其拆分并添加在一起即可;

permanent solution would be for this is getting long string from file specially when you are writing test case in spring projects .永久的解决方案是,当您在spring 项目中编写测试用例时,特别是从文件中获取长字符串。 the below one worked for me下面的对我有用

File resource = new ClassPathResource(
      "data/employees.dat").getFile();
    String employees = new String(
      Files.readAllBytes(resource.toPath()));

你可以试试这个

public static final String CONSTANT = new StringBuilder("Your really long string").toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 String太长时,Ant会引发错误 - Ant throws error when String is too long 使用Ant但不使用Eclipse在Java中导入错误 - Import Error in Java Using Ant but not Eclipse Java常量字符串太长异常-运行时 - Java constant string too long Exception - Runtime 用ant编译java代码 - Using ant to compile java code 蚂蚁建立错误。 在Eclipse上成功但未使用ant。 “找不到符号”和“类……无法应用于给定类型”错误 - Ant build error. Succeful on Eclipse but not using ant. “cannot find symbol” and “class … cannot be applied to given types” error 我如何声明一个大字符串( <Html> 代码)中避免“常量字符串过长”的错误? - How can I declare a Large String(<Html> codes) in Java to avoid “Constant string too long” error? Ant中的Java编译错误:在注释中使用类文字 - Java compile error in Ant: using class literals in annotations Eclipse 无法打开并给我一个很长的错误。 [爪哇] - Eclipse fails to open and gives me a very long error. [Java] 在Eclipse中使用ANT脚本时如何调试Java代码 - How to debug Java code when using ANT script in Eclipse 执行以下代码时,firefox显示错误。 我正在使用最新版本的java,eclipse,firefox和WebDrive jar文件 - When the below code is executed, firefox is showing error. I am using the latest version of java, eclipse, firefox, and WebDrive jar file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM