-->

“Code too large” compilation error in Java

2019-01-01 01:43发布

问题:

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.

        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?

回答1:

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

But you should clean this up!

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

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

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


回答2:

There is a 64K byte-code size limit on a method

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.



回答3:

According to the Java Virtual Machine specification, a the code of a method must not be bigger than 65536 bytes:

The value of the code_length item must be less than 65536.

Where code_length is defined in §4.7.3 The Code Attribute:

code_length: The value of the code_length item gives the number of bytes in the code array for this method. The value of code_length must be greater than zero; the code array must not be empty.

code[]: The code array gives the actual bytes of Java virtual machine code that implement the method.



回答4:

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?



回答5:

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



回答6:

As mentioned in other answers there is a 64KB of bytecode limit for a method (at least in Sun\'s java compiler)

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)

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



回答7:

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.

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



回答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.



回答9:

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