java javac 错误“代码太大”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/243097/
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
javac error "code too large"?
提问by Epaga
I have a unit test where I have statically defined a quite large byte array (over 8000 bytes) as the byte data of a file I don't want to read every time I run my unit test.
我有一个单元测试,其中我静态定义了一个相当大的字节数组(超过 8000 个字节)作为我不想在每次运行单元测试时读取的文件的字节数据。
private static final byte[] FILE_DATA = new byte[] {
12,-2,123,................
}
This compiles fine within Eclipse, but when compiling via Ant script I get the following error:
这在 Eclipse 中编译得很好,但是当通过 Ant 脚本编译时,我收到以下错误:
[javac] C:\workspace\CCUnitTest\src\UnitTest.java:72: code too large
[javac] private static final byte[] FILE_DATA = new byte[] {
[javac] ^
Any ideas why and how I can avoid this?
任何想法为什么以及如何避免这种情况?
Answer: Shimi's answer did the trick. I moved the byte array out to a separate class and it compiled fine. Thanks!
回答: Shimi 的回答成功了。我将字节数组移到了一个单独的类中,并且编译得很好。谢谢!
回答by Shimi Bandiel
回答by Cem Catikkas
You can load the byte array from a file in you @BeforeClassstatic method. This will make sure it's loaded only once for all your unit tests.
您可以在@BeforeClass静态方法中从文件加载字节数组。这将确保它只为您的所有单元测试加载一次。
回答by Josh
You can leverage inner classes as each would have it's own 64KB limit. It may not help you with a single large array as the inner class will be subject to the same static initializer limit as your main class. However, you stated that you managed to solve the issue by moving your array to a separate class, so I suspect that you're loading more than just this single array in your main class.
您可以利用内部类,因为每个内部类都有自己的 64KB 限制。它可能无法帮助您处理单个大数组,因为内部类将受到与主类相同的静态初始值设定项限制。但是,您表示您通过将数组移动到一个单独的类设法解决了这个问题,所以我怀疑您在主类中加载的不仅仅是这个单个数组。
Instead of:
代替:
private static final byte[] FILE_DATA = new byte[] {12,-2,123,...,<LARGE>};
Try:
尝试:
private static final class FILE_DATA
{
private static final byte[] VALUES = new byte[] {12,-2,123,...,<LARGE>};
}
Then you can access the values as FILE_DATA.VALUES[i]instead of FILE_DATA[i], but you're subject to a 128KB limit instead of just 64KB.
然后您可以访问值 asFILE_DATA.VALUES[i]而不是FILE_DATA[i],但您受到 128KB 的限制,而不仅仅是 64KB。

