[英]"Code too large" compilation error in Java
Is there any maximum size for code in Java? Java中的代码有最大大小吗? I wrote a function with more than 10,000 lines.我写了一个超过 10,000 行的函数。 Actually, each line assigns a value to an array variable.实际上,每一行都为数组变量赋值。
arts_bag[10792]="newyorkartworld";
arts_bag[10793]="leningradschool";
arts_bag[10794]="mailart";
arts_bag[10795]="artspan";
arts_bag[10796]="watercolor";
arts_bag[10797]="sculptures";
arts_bag[10798]="stonesculpture";
And while compiling, I get this error: code too large在编译时,我得到这个错误:代码太大
How do I overcome this?我该如何克服呢?
A single method in a Java class may be at most 64KB of bytecode. Java 类中的单个方法最多可以有 64KB 的字节码。
But you should clean this up!但是你应该把它清理干净!
Use .properties
file to store this data, and load it viajava.util.Properties
使用.properties
文件存储这些数据,并通过java.util.Properties
加载它
You can do this by placing the .properties
file on your classpath, and use:你可以通过将.properties
文件放在你的类路径上来做到这一点,并使用:
Properties properties = new Properties();
InputStream inputStream = getClass().getResourceAsStream("yourfile.properties");
properties.load(inputStream);
There is a 64K byte-code size limit on a method 方法有 64K 字节码大小限制
Having said that, I have to agree w/Richard;话虽如此,我不得不同意理查德。 why do you need a method that large?为什么需要这么大的方法? Given the example in the OP, a properties file should suffice ... or even a database if required.给定 OP 中的示例,一个属性文件就足够了……如果需要,甚至是一个数据库。
According to the Java Virtual Machine specification , the code of a method must not be bigger than 65536 bytes :根据Java 虚拟机规范, 方法的代码不得大于 65536 字节:
The value of the
code_length
item gives the number of bytes in thecode
array for this method.code_length
项的值给出了此方法的code
数组中的字节数。The value of code_length must be greater than zero (as the code array must not be empty) and less than 65536. code_length 的值必须大于零(因为代码数组不能为空)且小于 65536。
code_length
defines the size of the code[]
attribute which contains the actual bytecode of a method: code_length
定义了包含方法实际字节码的code[]
属性的大小:
The
code
array gives the actual bytes of Java Virtual Machine code that implement the method.code
数组给出了实现该方法的 Java 虚拟机代码的实际字节。
This seems a bit like madness.这似乎有点疯狂。 Can you not initialize the array by reading the values from a text file, or some other data source?您不能通过从文本文件或其他数据源中读取值来初始化数组吗?
This error sometimes occur due to too large code in a single function... To solve that error, split that function in multiple functions, like由于单个函数中的代码太大,有时会发生此错误...要解决该错误,请将该函数拆分为多个函数,例如
//Too large code function
private void mySingleFunction(){
.
.
2000 lines of code
}
//To solve the problem
private void mySingleFunction_1(){
.
.
500 lines of code
}
private void mySingleFunction_2(){
.
.
500 lines of code
}
private void mySingleFunction_3(){
.
.
500 lines of code
}
private void mySingleFunction_4(){
.
.
500 lines of code
}
private void MySingleFunction(){
mySingleFunction_1();
mySingleFunction_2();
mySingleFunction_3();
mySingleFunction_4();
}
Try to refactor your code.尝试重构您的代码。 There is limit on the size of method in Java. Java中方法的大小是有限制的。
As mentioned in other answers there is a 64KB of bytecode limit for a method (at least in Sun's java compiler)正如其他答案中提到的,方法有 64KB 的字节码限制(至少在 Sun 的 java 编译器中)
Too me it would make more sense to break that method up into more methods - each assigning certain related stuff to the array (might make more sense to use a ArrayList to do this)对我来说,将该方法分解为更多方法会更有意义 - 每个方法都将某些相关的东西分配给数组(使用 ArrayList 来执行此操作可能更有意义)
for example:例如:
public void addArrayItems()
{
addSculptureItems(list);
...
}
public void addSculptureItems(ArrayList list)
{
list.add("sculptures");
list.add("stonesculpture");
}
Alternatively you could load the items from a static resource if they are fixed like from a properties file或者,如果它们像从属性文件中一样被固定,您可以从静态资源加载项目
I have run into this problem myself.我自己也遇到过这个问题。 The solution that worked for me was to refactor and shrink the method to more manageable pieces.对我有用的解决方案是将方法重构并缩小为更易于管理的部分。 Like you, I am dealing with a nearly 10K line method.和你一样,我正在处理一个近 10K 行的方法。 However, with the use of static variables as well as smaller modular functions, the problem was resolved.然而,通过使用静态变量以及更小的模块化函数,问题得到了解决。
Seems there would be a better workaround, but using Java 8, there is none...似乎会有更好的解决方法,但是使用 Java 8,没有...
You can add another method to create space for your code for additional data space, you might have a method that is taking a large amount of data space.您可以添加另一种方法来为您的代码创建空间以获得额外的数据空间,您可能有一个占用大量数据空间的方法。 Try dividing your methods because I had the same issue and and fix it by creating another an additional method for the same data in my java Android code, The issue was gone after I did that.尝试划分您的方法,因为我有同样的问题,并通过在我的 java Android 代码中为相同的数据创建另一个额外的方法来修复它,在我这样做之后问题就消失了。
这是由于单一方法解决方案中的所有代码:创建更多一些小方法然后此错误将消失
I came to this question because I was trying to solve a similar problem.我来这个问题是因为我试图解决类似的问题。 I wanted to hard code a graph that had 1600 elements into a 2D integer array for performance reasons.出于性能原因,我想将具有 1600 个元素的图形硬编码到二维整数数组中。 I was solving a problem on a leetcode style website and loading the graph data from a file was not an option.我在一个 leetcode 风格的网站上解决了一个问题,从文件中加载图形数据不是一种选择。 The entire graph exceeded the 64K maximum so I could not do a single static run of assignments.整个图表超过了 64K 的最大值,所以我无法进行一次静态分配。 I split the assignments across several static methods each below the limit and then called each method one by one.我将分配分配给几个静态方法,每个方法都低于限制,然后一个一个地调用每个方法。
private static int[][] G = new int[1601][];
static {
assignFirst250();
assignSecond250();
assignSecond500();
assignThird500();
}
private static void assignFirst250() {
G[1] = new int[]{3,8,15,24,35,48,63,80,99,120,143,168,195,224,255,288,323,360,399,440,483,528,575,624,675,728,783,840,899,960,1023,1088,1155,1224,1295,1368,1443,1520,1599};
G[2] = new int[]{2,7,14,23,34,47,62,79,98,119,142,167,194,223,254,287,322,359,398,439,482,527,574,623,674,727,782,839,898,959,1022,1087,1154,1223,1294,1367,1442,1519,1598};
I have an enum that causes the .java file to be over 500KB in size.我有一个导致 .java 文件大小超过 500KB 的枚举。 Eclipse can build it for some reason; Eclipse 可以出于某种原因构建它; the eclipse-exported ant build.xml cannot. eclipse 导出的 ant build.xml 不能。 I'm looking into this and will update this post.我正在研究这个并将更新这篇文章。
As there is a size limit for methods and you don't want to redesign your code as this moment, may be you can split the array into 4-5 parts and then put them into different methods.由于方法有大小限制,并且此时您不想重新设计代码,因此您可以将数组拆分为 4-5 个部分,然后将它们放入不同的方法中。 At the time of reading the array, call all the methods in a series.在读取数组的时候,依次调用所有的方法。 You may maintain a counter as well to know how many indexes you have parsed.你也可以维护一个计数器来知道你已经解析了多少索引。
ok maybe this answer is too late but I think this way is better than another way so好的,也许这个答案为时已晚,但我认为这种方式比另一种方式更好
for example, we have 1000 rows data in code例如,我们在代码中有 1000 行数据
break them打破他们
private void rows500() { //you shoud write 1-500 rows here } private void rows1000() { you shoud write 500-1000 rows here }
for better performance put an "if" in your codes为了获得更好的性能,请在您的代码中添加“如果”
if (count < 500) { rows500(); } else if (count > 500) { rows1000(); }
I hope this code helps you我希望这段代码可以帮助你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.