Java“常量字符串太长”编译错误。仅在使用 Ant 时发生,在使用 Eclipse 时不会发生
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2738574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java "constant string too long" compile error. Only happens using Ant, not when using Eclipse
提问by Allan
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. Whenever I compile the same class with ant, I get the "constant string too long" compile error. 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 构建文件来使用。每当我用 ant 编译同一个类时,都会出现“常量字符串太长”编译错误。我尝试了多种方法来在 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 中获得相同的成功编译,而不是尝试重新编写代码以动态连接字符串。
采纳答案by teabot
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:
查看:
回答by Yishai
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. In order to use the Eclipse compiler, you simply need to define the build.compiler property in your script. Here is a small example.
使用 ant javac 适配器 Eclipse 编译器可以使用 javac 适配器在 Ant 脚本中使用。为了使用 Eclipse 编译器,您只需在脚本中定义 build.compiler 属性。这是一个小例子。
<?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 编译项目都会真正开始限制你很多方法。
回答by J?rn Horstmann
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. Perhaps you are using a different character set in your ant file than in eclipse, so that some characters need more bytes than before. Please check the encoding
attribute of your javac
task.
类文件中字符串常量的长度在 UTF-8 编码中限制为 2^16 字节,这不应依赖于所使用的编译器。也许您在 ant 文件中使用了与 eclipse 中不同的字符集,因此某些字符需要比以前更多的字节。请检查encoding
您的javac
任务的属性。
回答by mrswadge
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!!!"
} );
回答by Monis Majeed
String theString2 = IOUtils.toString(new FileInputStream(new
File(rootDir + "/properties/filename.text")), "UTF-8");
回答by df778899
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. Thisdescribes 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 中不是问题。
回答by xevser
Add your string to values/strings.xml than call getResources.getString(R.string.yourstring)
将您的字符串添加到 values/strings.xml 中,而不是调用 getResources.getString(R.string.yourstring)
回答by Johjen K Mathew
You can try this,
你可以试试这个
public static final String CONSTANT = new StringBuilder("Your really long string").toString();
回答by Amol Suryawanshi
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")));
回答by Lukas Eder
A workaround is to chunk your string using new String()
(yikes) or StringBuilder
, e.g.
一种解决方法是使用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 e.g. from a code generator. The workaround documented in this answerwhere string literals are concatenated no longer works with Java 11
如果您从代码生成器生成此字符串,这些可能是可行的选择。此答案中记录的解决方法在连接字符串文字时不再适用于 Java 11