I am using Netbeans, it's just a simple code and i am trying to initialize my array named as primes, which will consist of about 100000 integers, as,
int[] primes = {0, 0, 1, 1, 1, 1, 2.......................................};
as far as I have noticed this error is arising because of large array. How can i get rid of this "could not find or load main class" error?
I should point out that an array initializer that lists 100,000 elements is not going to compile. The Java classfile specification places limits on certain things, and one of them is that the code array for a method cannot be larger than 65535 bytes. An array initializer with 100,000 values is going the exceed that limit ... easily.
Reference: JVM spec 4.9.1.
(The spec mentions the code size limit explicitly. Other limits don't get this treatment; e.g. the number of constants in the constant pool, the number of methods in a class, and so on. Either way, the limits are inherent in the sizes of various fields in the classfile format.)
I recommend that you put the primes into a resource in your JAR file, and read them into an array allocated with the correct size.
FOLLOW-UP
"I have also tried keeping the numbers in array in a string separated by spaces [...] So, is there a limitation on the size of String as well?"
Yes. A string literal can only be up to 65535 bytes long in (modified) UTF-8 encoding; see JVM spec 4.4.3 and 4.4.7
(That's why I didn't recommend that approach ...)
It is possible that this is actually causing your "cannot find main" problem ... but only if your are IGNORING the compilation error and trying to run the code anyway. If you are doing that, you should change your IDE preferences so that you don't accidentally try to run code that has compilation errors. (It will save you time / pain debugging bogus runtime errors that are really compilation errors ... like this one.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With