views:

775

answers:

7

Hello all,

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_bag[10792]="newyorkartworld";
        arts_bag[10793]="leningradschool";
        arts_bag[10794]="mailart";
        arts_bag[10795]="artspan";
        arts_bag[10796]="watercolor";
        arts_bag[10797]="sculptures";
        arts_bag[10798]="stonesculpture";  

And while compiling , i get this error : code too large

How do i overcome this ?

+6  A: 

This seems a bit like madness. Can you not initialize the array by reading the values from a text file, or some other data source?

RichardOD
+15  A: 

this thread says

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);
Bozho
"No matter what the actual size for your JDK/JVM is"? Are you implying that this limit is not fixed? Because it is fixed and required by the class file format specification.
Joachim Sauer
I did not know it is fixed, thanks for the clarification.
Bozho
where can i find the .properties file
trinity
you create your own then put it on the classpath
Mark
@trinity - did you manage to make it work?
Bozho
i didnt use your suggestion , though i am eager to try it next time.. have now used a database to store this information , and 've modified the rest of the code accordingly..
trinity
@trinity yes, a database is a good option as well. Still, you are free to mark the answer as accepted, I guess :)
Bozho
+4  A: 

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.

Everyone
+2  A: 

Try to refactor your code. There is limit on the size of method in Java.

Padmarag
refactoring doesn't seam as a reasonable idea if all he does in that method is array initialization.
Gabriel Ščerbák
He said that the rest of his code depends on this being an array. So he could refactor the method to pass the responsibility of loading data to some other method/Factory from file/database.
Padmarag
@Padmarag - you can create and initialize large arrays without resorting to this kind of nonsense; see @Kris's answer.
Stephen C
Refactor - "Process of changing a computer program's source code without modifying its external functional behavior in order to improve some of the nonfunctional attributes of the software." How are other answers different from this?
Padmarag
+1  A: 

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

saret
+2  A: 

According to the Java Virtual Machine specification, a the code of a method must not be bigger than 65536 bytes:

The value of the code_length item must be less than 65536.

Where code_length is defined in §4.7.3 The Code Attribute:

code_length: The value of the code_length item gives the number of bytes in the code array for this method. The value of code_length must be greater than zero; the code array must not be empty.

code[]: The code array gives the actual bytes of Java virtual machine code that implement the method.

Joachim Sauer
+3  A: 

Wow, that's bad.

Why this is bad has already been explained. There are a number of ways to fix this but the following should not break any other code.

public static String[] getTheArrayThing(){
    List<String> list = new LinkedList<String>();
    try {
        BufferedReader br = new BufferedReader(new FileReader(<the file>));
        String line = br.readLine();
        while (line != null) {
            list.add(line);
            line = br.readLine();
        }

    } catch (Exception e){
        throw new IllegalStateException("Couldn't load array file");
    }
    return list.toArray(new String[0]);
}

Bascially the method creates (and returns but it could just as easily edit a class variable) an array that contains each line of file in order.

Just dump the values in your source example (e.g. "newyorkartworld") to a text file - in order - and use this to load the array.

While this simple drop in replacement will work, there are far more elegant solution out there if you can find the time to do a little refactoring.

Kris