Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javac error "code too large"?

Tags:

I have a unit test where I have statically defined a quite large byte array (over 8000 bytes) as the byte data of a file I don't want to read every time I run my unit test.

private static final byte[] FILE_DATA = new byte[] {
12,-2,123,................
}

This compiles fine within Eclipse, but when compiling via Ant script I get the following error:

[javac] C:\workspace\CCUnitTest\src\UnitTest.java:72: code too large
[javac]     private static final byte[] FILE_DATA = new byte[] {
[javac]                                 ^

Any ideas why and how I can avoid this?


Answer: Shimi's answer did the trick. I moved the byte array out to a separate class and it compiled fine. Thanks!

like image 782
Epaga Avatar asked Oct 28 '08 12:10

Epaga


People also ask

How do I fix too big for Java?

So, refactoring the erring method into several smaller methods will fix the problem for us. In the case of array initializations, we can either split the arrays or load from a file. We can also use static initializers. Even when we're using code generators, we can still refactor the code.

How do I fix compile errors in Java?

If the brackets don't all match up, the result is a compile time error. The fix to this compile error is to add a leading round bracket after the println to make the error go away: int x = 10; System.

What do methods do?

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions.


1 Answers

Methods in Java are restricted to 64k in the byte code. Static initializations are done in a single method (see link)
You may try to load the array data from a file.

like image 154
Shimi Bandiel Avatar answered Oct 06 '22 01:10

Shimi Bandiel