35

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
zb226
  • 9,586
  • 6
  • 49
  • 79
The Learner
  • 3,867
  • 14
  • 40
  • 50
  • 3
    `The amount of code per non-native, non-abstract method is limited to 65536 bytes by the sizes of the indices in the exception_table of the Code attribute (§4.7.3), in the LineNumberTable attribute (§4.7.8), and in the LocalVariableTable attribute (§4.7.9).` according to java doc http://docs.oracle.com/javase/specs/#88659 – Dinup Kandel Sep 04 '12 at 05:47
  • is it 9000 or 90,000 ? – Tom Taylor Dec 06 '16 at 16:18

8 Answers8

34

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.

zb226
  • 9,586
  • 6
  • 49
  • 79
Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37
15

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.

Infiltrator
  • 1,611
  • 1
  • 16
  • 25
  • why should I load froma file? all I am doing is having a array and do a print of that array? I have no issue in reading from a file but want to know why we should do that ...I really did not get what the array is – The Learner Sep 04 '12 at 05:47
  • 5
    There are several issues: firstly, you're hard-coding details, which is almost always a bad thing; secondly, you're getting that error message; thirdly, you make your code very unreadable. There are many more, of course, but these are the obvious ones. – Infiltrator Sep 04 '12 at 05:49
  • "arbitrary" may be a bit harsh? I *assume* the limitation exists to allow use of 16 bit indices. – tgdavies Oct 01 '22 at 00:40
3

Initialising the studentnames array is counting towards the size of the main method. As there are 9000 student names each name can only be about 7 characters before you'll run out of space. As the others have stated you need to reduce the size of method. You can split it into pieces as Pramod said but in this case the bulk of the method is actually data. I would do as Infiltrator says and split the names out into a separate file and just read it in your main. Something like commons-io can be used to get you to effectively the same position you're starting in.

List<String> namelist = FileUtils.readLines(new File("studentnames.txt"));
String[] studentnames = namelist.toArray(new String[0]);

You may find it useful to process the list rather than convert it to an array or alternatively you could use a LineIterator instead

LineIterator it = FileUtils.lineIterator(file);
try {
     while (it.hasNext()) {
         String line = it.nextLine();
         // do something with line
     }
 } finally {
     it.close();
 }
Michael Rutherfurd
  • 13,815
  • 5
  • 29
  • 40
2

I would read the students names from a file however one work around which will make the class smaller as well

String[] studentnames= "Student names is an array of :9000".split(" ");

Instead of defining and using 9000 strings, this uses just one. The limit for a single String is just over 2 billion.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • This produces the same error: `The code for the static initializer is exceeding the 65535 bytes limit`. – minipif Aug 21 '14 at 07:32
  • @minipif Can you explain why that is because it doesn't for me. The String is not in a code block so it doesn't effect the code block size. – Peter Lawrey Aug 23 '14 at 21:02
  • I tried to create a string containing the first 100000 prime numbers separated by spaces, this string has length 710485. I declare it has `private static final` but get the error `The type generates a string that requires more than 65535 bytes to encode in Utf8 format in the constant pool`. It appears that you can't have a string literal (String constant embedded in the source code) of length more than 65535. So reading it from a file is mandatory in that case. – minipif Aug 25 '14 at 06:55
  • @minipif Correct but this is a different error. If you create an array fo about 4000 bytes you will get a code block size error. With a String you can get say 65535 bytes. – Peter Lawrey Aug 25 '14 at 11:35
1

You method is too long.

Create different methods to split it and make it much more readable and maintanable.

Jagger
  • 10,350
  • 9
  • 51
  • 93
1

Solution number 1:

public static void main(String[] args) {
    int[] num ={100001,100002,.......,...};
}

Suppose the array num contains 100000 numbers, and your main function gives an error. In such scenario, you can split the above array in two parts. Now, you can declare 2 different arrays instead of just a single array. For example:

static int[] num1 ={} and static int[] num2={}

Declare num1 outside of the main function and num2 inside the main function where num2 will replace num inside the main function.

All elements of the array num will be split into two subarrays num1 and num2; split in such a way that both a static and non static array could hold say 8000 and 2000 elements.

The code inside the main method can handle all 10000 elements, an assumption only:

if (n <= 8000) {
    System.out.println(" --->  " + num2[((int) n - 1)]);
} else if (n > 8000 && n <= 10000) {
    n = n - 8000;
    System.out.println(" --->  " + num1[((int) n - 1)]);
}

So splitting that array or say dividing the elements in two parts of 8000 and 2000, could remove the error.

Solution number 2:

Put the array outside main method, both num1 and num2 are now static arrays but the 65535 bytes limit holds also for static initialised arrays. Split your elements, for example, you can split 10000 in 8000 and 2000, or as per you requirement in two static arrays outside the main method.

This error is related to the storage of elements in long[] or int[] where the error occurs if the storage size surpasses the limit of 65535. In such a scenario go splitting the elements, for example if you are using long[] or int[] split them in suitable numbers till the error gets resolved.

In other words, it is not mandatory to only split the array into 2 parts, you can divide them in 4 different sizes also or different equal parts. But again the solution to your problem may vary as per your requirement.

Neph
  • 1,823
  • 2
  • 31
  • 69
  • divide the array num in above scenario into 2 different array for example int[] num ={}; will become int[] num ={} and int[] num1={}; and distribute your elements into 2 parts in these different arrays. – natwar kumar Jun 17 '19 at 08:48
  • You can [edit] your answer and include there that part from comment. Please read [how to improve your answer](https://stackoverflow.com/help/how-to-answer) - it's a bit chaotic – barbsan Jun 17 '19 at 09:15
0

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

As your error says the main method exceeds the limit of 65535 bytes.

Reason : Why we got this error in our code?

  • We know that the memory allocation for array would be done sequentially. ie; the memory would be allocated for the array if such a huge amount of space is available in RAM continuously.
  • We have used String array which holds some "9000" strings - which is too large to hold in the memory
  • The RAM might not have such a huge space continuously during that time so, we might have got that error.

Solution : So what can I do for this?

  • For storing such huge size value we may prefer file for read/write - so we can read a part of a file when required
  • We can store those strings in an ArrayList which grows its memory when element gets added to it instead of allocating memory on a whole initially.
Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
0

Just externalize Strings from your code to one *.properties / xml / json file. Netbeans and/or Eclipse can do it for you easly.