Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add jars to SpringBoot at runtime

I'm using SpringBoot 1.4.2. I want to be able to launch my SpringBoot application from the command line, specifying additional, external jars that should also be component scanned at the time I launch the application. I can't use the spring-boot-maven-plugin because I won't have the spring boot application's pom when I want to launch it.

I have tried the following so far, where my-jar.jar is the spring boot application and external-jar.jar is the external jar I want to include on the command line:

java -cp my-jar.jar -Dloader.path=external-jar.jar -Dloader.main=com.my.Application org.springframework.boot.loader.JarLauncher

This launches but does not component scan external-jar.jar

java -cp external-jar.jar:my-jar.jar -Dloader.main=com.my.Application org.springframework.boot.loader.JarLauncher

This fails to launch, saying @Autowired conditions in external-jar.jar were not satisfied, even though classes in my-jar.jar do satisfy them. I have tried reversing the order of the jars on the classpath, this doesn't fix the issue.

java -Dloader.path="external-jar.jar" -jar my-jar.jar

This launches but does not component scan external-jar.jar

My component scanning path at the top of my SpringApplication class has a package paths for both my-jar.jar and external-jar.jar and I have verified that when referencing external-jar.jar directly within my pom it is component scanned.

like image 609
user783836 Avatar asked Nov 03 '25 15:11

user783836


1 Answers

You're using the wrong launcher. Support for loader.path and loader.main is provided by PropertiesLauncher but you're using JarLauncher.

like image 113
Andy Wilkinson Avatar answered Nov 06 '25 10:11

Andy Wilkinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!