Search code examples
javafile-iofilesize

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?


Solution

  • 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.