user1581900
user1581900

Reputation: 3720

Is there a limit on how big a java file can be?

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?

Upvotes: 1

Views: 417

Answers (4)

linski
linski

Reputation: 5094

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

Also, take a look at this discussion.

Upvotes: 1

Rytek
Rytek

Reputation: 563

Upvotes: 2

juanignaciosl
juanignaciosl

Reputation: 3573

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

Nevertheless, you should split it into different methods.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533492

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.

Upvotes: 9

Related Questions