Menu

#29 Issues with embedded JAR files

open
nobody
None
5
2012-05-04
2012-05-04
Anonymous
No

Hi,

I tried embedding my app's main JAR file in the EXE and found the following issues:

1) The app is slow to load. Just to give you a rough example: for a 1 MB JAR file, the app starts up in about 1 second when the JAR is not embedded, and about 5 seconds when the JAR is embedded. So the slowdown is approx 5x - I'm sure it would be more or less depending on the number of classes in the JAR, etc. Taking a look at EmbeddedClassLoader.java, I suspect the problem is that getResourceAsStream() and findClass() have to enumerate through *all* entries in the JAR file every time a class or resource is loaded. Ideally they would use a class like ZipFile that maintains an index of what's in the JAR and can do quick lookups. Unfortunately ZipFile has an ancient badly-designed API and only works with actual files and not streams or byte arrays (arghhh!). A temporary workaround would be to extract and cache byte arrays for all entries in the JAR up front. This would be slower than doing a ZipFile-style lookup, but should be a lot faster than the current code.

2) When the JAR is embedded, calling myObject.getClass().getClassLoader().getResource(...) always returns a NULL URL. A workaround is to use getResourceAsStream() instead. However, this workaround won't help if you're embedding some 3rd party JARs or classes that use getResource().

I realize that the web site says embedding JAR files is currently BETA quality, so you may already know about these issues. But just mentioning them here in case you didn't.

Regards,
Daniel

Discussion

MongoDB Logo MongoDB