Search code examples
javaspring-boottomcat

Spring-boot inner jar files loading order? (embedded tomcat)


I have a spring-boot "fat" jar (1.5.22) with embedded tomcat (tomcat-embed-core-8.5.57.jar).

Inside the jar - there are many "inner" jars under BOOT-INF/lib.

In what order are those inner jars loaded by the class-loader?

If I run java -jar myFatSpringBoot.jar on multiple computers - will inner jars be picked up in the same exact order, or does it "depend" on something on each computer? (FS/tmpfs/java/etc)


UPDATE: many responders indicated it takes inner jars in the exact order in which they appear in the fat jar (so at least the QA and PROD should behave same way for same fat jar, unlike it was for non-spring-boot .war apps).

Now I'm wondering if we have any machinery to preserve/enforce/specify the order of inner jars during the process of creating/packaging the jar? (maven/gradle/...)


Solution

  • The jars in BOOT-INF/lib are always added to Spring Boot’s class loader in the order in which they appear in the jar. This means that if you have the same class file or resource declared in multiple jars, the same one will always win across multiple runs of your application, irrespective of operating system, Java version, etc. This holds true even if you're using Spring Boot's support for automatically unpacking certain jars at runtime.

    Spring Boot also packages a BOOT-INF/classpath.idx index file in each jar that it builds. It lists all of the nested jars "in the order that they should be added to the classpath". If you unzip the jar file such that the jars in BOOT-INF/lib are then ordered by the filesystem, if you continue to use Spring Boot's JarLauncher to launch the application it will honour the order in the index.

    Spring Boot contains tests, such as these, that verify the ordering of the classpath, both when launching an application using java -jar and when using java org.springframework.boot.loader.JarLauncher.