“Code too large” compilation error in Java

2019-01-01 00:51发布

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?

9条回答
裙下三千臣
2楼-- · 2019-01-01 01:28

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);
查看更多
初与友歌
3楼-- · 2019-01-01 01:29

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.

查看更多
像晚风撩人
4楼-- · 2019-01-01 01:34

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.

查看更多
冷夜・残月
5楼-- · 2019-01-01 01:35

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

查看更多
妖精总统
6楼-- · 2019-01-01 01:37

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

查看更多
梦该遗忘
7楼-- · 2019-01-01 01:39

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.

查看更多
登录 后发表回答