Code Compilation Error Too Big in Java

Is there a maximum code size in Java? I wrote a function with over 10,000 lines. In fact, each row 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 at compile time I get this error: the code is too big

How can I overcome this?

+79
java arrays compiler-errors code-size
Mar 09 '10 at 9:33
source share
10 answers

One method in a Java class can contain no more than 64 Kbytes of bytecode.

But you have to clear it!

Use the .properties file to store this data and load it through java.util.Properties

You can do this by placing the .properties file in your classpath and using:

 Properties properties = new Properties(); InputStream inputStream = getClass().getResourceAsStream("yourfile.properties"); properties.load(inputStream); 
+79
Mar 09 '10 at 9:35
source share

64K byte size limit on

Having said that, I must agree with Richard; why do you need such a method? Given the example in OP, a properties file should be sufficient ... or even a database if necessary.

+12
Mar 09
source share

According to the Java Virtual Machine specification , the code method should not exceed 65536 bytes :

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

Where code_length defined in Β§4.7.3 Code attribute :

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

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

+10
Mar 09 2018-10-09T00:
source share

This is like madness. Can you initialize the array by reading values ​​from a text file or other data source?

+9
Mar 09 '10 at 9:35
source share

Try reorganizing your code. There is a method size limit in Java.

+3
Mar 09 '10 at 9:39
source share

Wow, this is bad. Hard coding data in your program is never a good idea in the first place. Hard coding a ton of data is no better, and you are faced with the limitations described in other answers.

There are several ways to fix this, but the following should not violate any other code.

 public static String[] getTheArrayThing(){ List<String> list = new LinkedList<String>(); try { BufferedReader br = new BufferedReader(new FileReader(<the file>)); String line = br.readLine(); while (line != null) { list.add(line); line = br.readLine(); } } catch (Exception e){ throw new IllegalStateException("Couldn't load array file"); } return list.toArray(new String[0]); } 

Basically, the method creates (and returns, but can just as easily edit the class variable) an array that contains each line of the file in order.

Just upload the values ​​in the original example (for example, "newyorkartworld") to a text file - in order - and use this to load the array.

While this simple substitution will work, there is a much more elegant solution if you can take the time to do a little refactoring.

+3
Mar 09
source share

As mentioned in other answers, there is a 64K byte limit for the method (at least in the Java compiler)

It would be far more reasonable for me to break this method down into a larger number of methods - each assigns an array to a certain related material (it may make sense to use an ArrayList for this)

eg:

 public void addArrayItems() { addSculptureItems(list); ... } public void addSculptureItems(ArrayList list) { list.add("sculptures"); list.add("stonesculpture"); } 

Alternatively, you can load elements from a static resource if they are fixed as from a properties file

+2
Mar 09 '10 at 9:44
source share

I ran into this problem myself. The solution that worked for me was to reorganize and compress the method into more manageable parts. Like you, I am dealing with a 10K line method. However, using static variables and small modular functions, the problem was resolved.

It seems like a workaround would be better, but using Java 8 no ...

+1
Sep 26 '14 at 23:31
source share

You can add another way to create space for your code for additional data space; you may have a method that takes up a large amount of data space. Try to share your methods because I had the same problem and fix it by creating another additional method for the same data in my Android java code. The problem disappeared after I did this.

+1
Dec 23 '15 at 11:05
source share

This error sometimes occurs because of too much code in one function ... To solve this error, divide this function into several functions, for example

 //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(); } 
0
Jul 23 '17 at 5:54 on
source share



All Articles