E.2 Spring Boot’s “JarFile” class
The core class used to support loading nested jars is org.springframework.boot.loader.jar.JarFile
. It allows you to load jar content from a standard jar file, or from nested child jar data. When first loaded, the location of each JarEntry
is mapped to a physical file offset of the outer jar:
myapp.jar +-------------------+-------------------------+ | /BOOT-INF/classes | /BOOT-INF/lib/mylib.jar | |+-----------------+||+-----------+----------+| || A.class ||| B.class | C.class || |+-----------------+||+-----------+----------+| +-------------------+-------------------------+ ^ ^ ^ 0063 3452 3980
The example above shows how A.class
can be found in /BOOT-INF/classes
in myapp.jar
position 0063
. B.class
from the nested jar can actually be found in myapp.jar
position 3452
and C.class
is at position 3980
.
Armed with this information, we can load specific nested entries by simply seeking to the appropriate part of the outer jar. We don’t need to unpack the archive and we don’t need to read all entry data into memory.
E.2.1 Compatibility with the standard Java “JarFile”
Spring Boot Loader strives to remain compatible with existing code and libraries. org.springframework.boot.loader.jar.JarFile
extends from java.util.jar.JarFile
and should work as a drop-in replacement. The getURL()
method will return a URL
that opens a java.net.JarURLConnection
compatible connection and can be used with Java’s URLClassLoader
.