“Code too large” compilation error in Java

后端 未结 13 1194
半阙折子戏
半阙折子戏 2020-11-22 14:33

Is there any maximum size for code in Java? I wrote a function with more than 10,000 lines. Actually, each line assigns a value to an array variable.

arts_b         


        
相关标签:
13条回答
  • 2020-11-22 14:49

    This error sometimes occur due to too large code in a single function... To solve that error, split that function in multiple functions, like

    //Too large code function
    private void mySingleFunction(){
    .
    .
    2000 lines of code
    }
    //To solve the problem
    private void mySingleFunction_1(){
    .
    .
    500 lines of code
    }
    private void mySingleFunction_2(){
    .
    .
    500 lines of code
    }
    private void mySingleFunction_3(){
    .
    .
    500 lines of code
    }
    private void mySingleFunction_4(){
    .
    .
    500 lines of code
    }
    private void MySingleFunction(){
    mySingleFunction_1();
    mySingleFunction_2();
    mySingleFunction_3();
    mySingleFunction_4();
    }
    
    0 讨论(0)
  • 2020-11-22 14:49

    I have run into this problem myself. The solution that worked for me was to refactor and shrink the method to more manageable pieces. Like you, I am dealing with a nearly 10K line method. However, with the use of static variables as well as smaller modular functions, the problem was resolved.

    Seems there would be a better workaround, but using Java 8, there is none...

    0 讨论(0)
  • A single method in a Java class may be at most 64KB of bytecode.

    But you should clean this up!

    Use .properties file to store this data, and load it via java.util.Properties

    You can do this by placing the .properties file on your classpath, and use:

    Properties properties = new Properties();
    InputStream inputStream = getClass().getResourceAsStream("yourfile.properties");
    properties.load(inputStream);
    
    0 讨论(0)
  • 2020-11-22 14:51

    ok maybe this answer is too late but I think this way is better than another way so

    for example, we have 1000 rows data in code

    1. break them

      private void rows500() {
           //you shoud write 1-500 rows here
      }
      
      private void rows1000() {
           you shoud write 500-1000 rows here
      }
      
    2. for better performance put an "if" in your codes

      if (count < 500) {
          rows500();
      } else if (count > 500) {
          rows1000();
      }
      

    I hope this code helps you

    0 讨论(0)
  • 2020-11-22 14:59

    There is a 64K byte-code size limit on a method

    Having said that, I have to agree w/Richard; why do you need a method that large? Given the example in the OP, a properties file should suffice ... or even a database if required.

    0 讨论(0)
  • 2020-11-22 14:59

    As mentioned in other answers there is a 64KB of bytecode limit for a method (at least in Sun's java compiler)

    Too me it would make more sense to break that method up into more methods - each assigning certain related stuff to the array (might make more sense to use a ArrayList to do this)

    for example:

    public void addArrayItems()
    {
      addSculptureItems(list);
      ...
    }
    
    public void addSculptureItems(ArrayList list)
    {
      list.add("sculptures");
      list.add("stonesculpture");
    }
    

    Alternatively you could load the items from a static resource if they are fixed like from a properties file

    0 讨论(0)
提交回复
热议问题