Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting around the 4GB JAR limit

I'm making a offline database program that happens to include a lot of images - enough to make the jar file over 4GB, which, as Netbeans tells me, is not permitted.

I would like to know what the best way of getting my files under this limit might be, and how to program in methods to access the images with Java. The image folder is currently 4.63GB by itself and will get larger (by maybe a few MBs) in future updates.

What do you suggest, Stackoverflow?

like image 417
Nathaniel D. Hoffman Avatar asked Nov 27 '14 20:11

Nathaniel D. Hoffman


1 Answers

You can create multiple jars. Why does it have to be one single jar?

Also starting with Java 1.7.0 build 55 Java supports jars and zip files larger than 4GB, see this post:

ZIP64, The Format for > 4G Zipfile, Is Now Supported

You can also compress the images, use a different format which compresses them to smaller size so altogether they will be less than 4GB; consider lossy compression like JPEG. If they are already maximum compressed, then you may consider decreasing their dimensions (e.g. 500x250 pixels instead of 1000x500).

Edit:

Quoting form the javadoc of java.util.zip package:

An implementation may optionally support the ZIP64(tm) format extensions defined by the PKWARE ZIP File Format Specification. The ZIP64(tm) format extensions are used to overcome the size limitations of the original ZIP format.

So it looks it's up to the java implementation whether it supports Zip64, it is not a specification requirement. The first link suggests though that OpenJDK supports it.

Also Googleing around a bit it looks all Oracle JDKs support it after the mentioned release (1.7.0 b55). Netbeans might deny to create jars larger than 4 GB if it's an old version or because it wants to keep compatibility with older JVMs.

How to tell if your Java supports it?

In case of Oracle JRE/JDK: Look for the rt.jar (runtime.jar) file of the JRE/JDK. Open it (it's a zip file) and go into the java/util/zip folder. If it has a ZipConstants64.class file, then it has support for Zip64.

From Java code: try to acquire the Class object for the mentioned class. If it succeeds, your Oracle JRE/JDK supports it:

System.out.println("Java version: " + System.getProperty("java.version"));
System.out.println("Java vendor: " + System.getProperty("java.vendor"));
try {
    Class.forName("java.util.zip.ZipConstants64");
    System.out.println("Your Java supports Zip64 :)");
} catch (ClassNotFoundException e) {
    System.out.println("Your Java doesn't support Zip64 :(");
}

Output:

Java version: 1.8.0_20
Java vendor: Oracle Corporation
Your Java supports Zip64 :)
like image 96
icza Avatar answered Sep 28 '22 19:09

icza