"Code too large" compilation error in Java
JavaArraysCompiler ErrorsCode SizeJava Problem Overview
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?
Java Solutions
Solution 1 - Java
> 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 http://java.sun.com/javase/6/docs/api/java/util/Properties.html">`java.util.Properties`</a>
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);
Solution 2 - Java
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.
Solution 3 - Java
According to the Java Virtual Machine specification, the code of a method must not be bigger than 65536 bytes:
> 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 (as the code array must not be empty) and less than 65536.
code_length
defines the size of the code[]
attribute which contains the actual bytecode of a method:
> The code
array gives the actual bytes of Java Virtual Machine code that implement the method.
Solution 4 - Java
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?
Solution 5 - Java
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();
}
Solution 6 - Java
Try to refactor your code. There is limit on the size of method in Java.
Solution 7 - Java
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
Solution 8 - Java
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...
Solution 9 - Java
You can add another method to create space for your code for additional data space, you might have a method that is taking a large amount of data space. Try dividing your methods because I had the same issue and and fix it by creating another an additional method for the same data in my java Android code, The issue was gone after I did that.
Solution 10 - Java
this is due to all code in single methods solution :create more some small methods then this error will be gone
Solution 11 - Java
I came to this question because I was trying to solve a similar problem. I wanted to hard code a graph that had 1600 elements into a 2D integer array for performance reasons. I was solving a problem on a leetcode style website and loading the graph data from a file was not an option. The entire graph exceeded the 64K maximum so I could not do a single static run of assignments. I split the assignments across several static methods each below the limit and then called each method one by one.
private static int[][] G = new int[1601][];
static {
assignFirst250();
assignSecond250();
assignSecond500();
assignThird500();
}
private static void assignFirst250() {
G[1] = new int[]{3,8,15,24,35,48,63,80,99,120,143,168,195,224,255,288,323,360,399,440,483,528,575,624,675,728,783,840,899,960,1023,1088,1155,1224,1295,1368,1443,1520,1599};
G[2] = new int[]{2,7,14,23,34,47,62,79,98,119,142,167,194,223,254,287,322,359,398,439,482,527,574,623,674,727,782,839,898,959,1022,1087,1154,1223,1294,1367,1442,1519,1598};
Solution 12 - Java
I have an enum that causes the .java file to be over 500KB in size. Eclipse can build it for some reason; the eclipse-exported ant build.xml cannot. I'm looking into this and will update this post.
Solution 13 - Java
As there is a size limit for methods and you don't want to redesign your code as this moment, may be you can split the array into 4-5 parts and then put them into different methods. At the time of reading the array, call all the methods in a series. You may maintain a counter as well to know how many indexes you have parsed.
Solution 14 - Java
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
-
break them
private void rows500() { //you shoud write 1-500 rows here } private void rows1000() { you shoud write 500-1000 rows here }
-
for better performance put an "if" in your codes
if (count < 500) { rows500(); } else if (count > 500) { rows1000(); }
I hope this code helps you