Java中的“代码太大”编译错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2407912/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:10:53  来源:igfitidea点击:

"Code too large" compilation error in Java

javaarrayscompiler-errorscode-size

提问by trinity

Is there any maximum size for code in Java? I wrote a function with more than 10,000 lines. Actually, each line assigns a value to an array variable.

Java 中的代码是否有最大大小?我写了一个超过 10,000 行的函数。实际上,每一行都为一个数组变量赋值。

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?

我如何克服这个问题?

采纳答案by Bozho

A single method in a Java class may be at most 64KB of bytecode.

Java 类中的单个方法最多可能有 64KB 的字节码。

But you should clean this up!

但你应该清理它!

Use .propertiesfile to store this data, and load it via java.util.Properties

使用.properties文件来存储这些数据,并通过java.util.Properties

You can do this by placing the .propertiesfile on your classpath, and use:

你可以通过将.properties文件放在你的类路径上来做到这一点,并使用:

Properties properties = new Properties();
InputStream inputStream = getClass().getResourceAsStream("yourfile.properties");
properties.load(inputStream);

回答by RichardOD

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?

这似乎有点像疯了。不能通过从文本文件或其他数据源读取值来初始化数组吗?

回答by Everyone

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 中的示例,属性文件应该就足够了……如果需要,甚至可以使用数据库。

回答by Padmarag

Try to refactor your code. There is limit on the size of method in Java.

尝试重构您的代码。Java 中方法的大小有限制。

回答by saret

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

或者,您可以从静态资源加载项目,如果它们像从属性文件一样固定

回答by Joachim Sauer

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_lengthitem gives the number of bytes in the codearray for this method.

The value of code_length must be greater than zero (as the code array must not be empty) and less than 65536.

项目的值code_length给出了code此方法的数组中的字节数。

code_length 的值必须大于零(因为代码数组不能为空)且小于 65536。

code_lengthdefines the size of the code[]attribute which contains the actual bytecode of a method:

code_length定义code[]包含方法的实际字节码的属性的大小:

The codearray gives the actual bytes of Java Virtual Machine code that implement the method.

code数组给出了实现该方法的 Java 虚拟机代码的实际字节。

回答by Azadi B.

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. However, with the use of static variables as well as smaller modular functions, the problem was resolved.

我自己也遇到过这个问题。对我有用的解决方案是重构并将方法缩小到更易于管理的部分。像您一样,我正在处理近 10K 行的方法。但是,通过使用静态变量以及较小的模块化函数,问题得到了解决。

Seems there would be a better workaround, but using Java 8, there is none...

似乎会有更好的解决方法,但是使用 Java 8,没有...

回答by FermDroi

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 代码中为相同的数据创建另一个额外的方法来修复它,在我这样做之后问题就消失了。

回答by MuhammadUmarFarooq

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();
}

回答by Richard Jessop

I have an enum that causes the .java file to be over 500KB in size. Eclipse can build it for some reason; the eclipse-exported ant build.xml cannot. I'm looking into this and will update this post.

我有一个枚举,导致 .java 文件的大小超过 500KB。Eclipse 可以出于某种原因构建它;eclipse 导出的 ant build.xml 不能。我正在调查这个并将更新这篇文章。