Alan Krueger
Alan Krueger

Reputation: 4786

Disadvantages to large resources in Java jar files?

Is there any particular, specific disadvantage to embedding large resource files in Java jar files that are deployed? (Ignoring space, bandwidth, and other simpler issues with the consequently larger jar file sizes, of course.)

That is, would having large resource files inside the jars make class loading slower, take up more memory within the JVM, or things like that?

A jar file is basically just a zip file and should have a central directory so it shouldn't have to scan the entire file for loading specific file in the archive. I don't think the jar class loaders load everything in a file, only the ones that are needed, but I could be mistaken.

Upvotes: 4

Views: 1488

Answers (1)

Adam Crume
Adam Crume

Reputation: 15824

Classes are loaded on demand. For normal JARs, very little is loaded eagerly (unless there are specific entries in the manifest, and I'm a bit rusty there, but most people don't use them). The JVM doesn't cache non-class resources in memory at all, so reading a resource from a JAR file is basically the same as manually reading an entry from a normal ZIP file. The only disadvantages I see are:

  1. The resource is copied every time the JAR is copied, even if it doesn't change, and
  2. The resource is read from a JAR file (which is compressed), even if it's something like an image which is already compressed.

I think ZIP files support per-file compression algorithms, and support a no-op compression algorithm, though, so the second point may not matter.

Upvotes: 4

Related Questions