Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "The code of method .. is exceeding the 65535 bytes limit"?

I have the following code:

public static void main(String[] args) {     try {         String[] studentnames = {             /* this is an array of 9000 strings... */         };     } } 

I get the following error when trying to compile this:

The code of method main(String[]) is exceeding the 65535 bytes limit 
like image 447
The Learner Avatar asked Sep 04 '12 05:09

The Learner


People also ask

Is exceeding the 65535 bytes limit Java?

If a subjob is too big, the size of the final generated code will exceed 65536 bytes. According to Java specifications, the amount of code for this non-native, non-abstract method is limited to a size of 65536 bytes, so the generated code of a subjob cannot be greater than this limit.

How many characters is 65 535 bytes?

A text column can be up to 65,535 bytes. An utf-8 character can be up to 3 bytes. So... your actual limit can be 21,844 characters.


2 Answers

In java a methods can't have more than 65535 bytes.

So to fix this problem, break up your main(String[] args) method in to multiple sub methods.

like image 106
Pramod Kumar Avatar answered Sep 19 '22 09:09

Pramod Kumar


The error code seems quite self-explanatory.

The code of method main(String[]) is exceeding the 65535 bytes limit  

This is because there is an arbitrary hard-coded limit in Java of 64Kb for method sizes. (And actually many other things are limited to 64K, such as method names, the number of constants, etc. See the Java 8 specs or the Java 7 specs for more details.)

To work around this, all you have to do is break up your main(String[] args) method into multiple sub-methods.


But why not just load the names from a file instead?

Some of the issues with doing it the way you are currently proposing are:

  • firstly, you're hard-coding details, which is almost always a bad thing (See this);

  • secondly, you're getting that error message; and

  • thirdly, you make your code very difficult to read.

There are many more, of course, but these are the obvious ones.

like image 24
Infiltrator Avatar answered Sep 19 '22 09:09

Infiltrator