1

I am developing a project where I am required to generate java code. I was doing some testing when I encountered a problem with 65535 bytes limit on the main method. My generated file looked something like that:

public class foo{
    public static void main(String[] args){
        //
        //7000 lines later
        //
    }
}

I thought that I'm being clever by generating the file this way:

public class foo{
    public static void bar(){
        //
        //7000 lines later
        //
    }
    public static void main(String[] args){
        bar();
    }
}

But I found out that 65535 bytes limit applies to every method.

I want to know if 'buffering' instructions into multiple methods will solve my problem. Is there any restriction on how big can the java file be?

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
user1581900
  • 3,680
  • 4
  • 18
  • 21
  • 4
    Is there any specific reason for writing such big main method? – Yogendra Singh Oct 05 '12 at 15:22
  • 3
    If you are writing all the code in the main method, you are doing it wrong. – nhahtdh Oct 05 '12 at 15:24
  • 3
    Having a method that has 7000+ lines would be a maintenance nightmare. It also shows that your code is not modular in nature. You should try to break your code into small, readable and logical methods. – Sujay Oct 05 '12 at 15:24
  • Take a look at this link http://weblogs.java.net/blog/editor/archive/2010/01/19/java-code-too-large-problem-reproduced-and-analyzed – RNJ Oct 05 '12 at 15:24
  • Is there any specific reason for writing such big `main`method? Per OOAD(Object Oriented analysis & Design) concepts, you should write modular. One method should not contain more than few(10s) lines. `7000` in one method doesn't sound right. – Yogendra Singh Oct 05 '12 at 15:25
  • There is a reason for such big file. We are making excel files. Really big ones. With Apache POI. After the java file is compiled and run it's being deleted never to be used again, so no one really is going to maintain the methods. – user1581900 Oct 05 '12 at 15:35

4 Answers4

9

A file can be any size, but a method cannot compile to more than 65535 bytes long.

The only way to solve this is to

  • use smaller methods, such a large method is unreadable.
  • if its generate code, create multiple methods.
  • externalise the information as a data file or database.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2
Community
  • 1
  • 1
Rytek
  • 563
  • 8
  • 22
1

It's a known bug/feature request (official Java bug tracker).

Nevertheless, you should split it into different methods.

juanignaciosl
  • 3,435
  • 2
  • 28
  • 28
1

I suppose that restriction exists and you have reached it - as stated here.

Also, take a look at this discussion.

Community
  • 1
  • 1
linski
  • 5,046
  • 3
  • 22
  • 35