Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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/...)

like image 980
Vlad Avatar asked Oct 29 '25 14:10

Vlad


2 Answers

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.

like image 167
Andy Wilkinson Avatar answered Oct 31 '25 06:10

Andy Wilkinson


I think the order will remain same. It's generally the JRE related classes and then the application. You can actually see the loading order yourself. Just launch the program with -verbose flag. Something like this

java -verbose -jar myFatSpringBoot.jar

This will print lots of information on what class is being loaded from which jar and which path. Apart from startup there will be few classes which will get loaded when your application is being used as generally the class-loading is lazy. You can log this info to an external file and later analyze.

java -verbose -jar myFatSpringBoot.jar > log.txt
like image 23
Shailendra Avatar answered Oct 31 '25 05:10

Shailendra