3

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.

user783836
  • 3,099
  • 2
  • 29
  • 34

1 Answers1

8

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

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • 1
    I honestly cannot thank you enough! `java -cp my-jar.jar -Dloader.path=external-jar.jar -Dloader.main=com.my.Application org.springframework.boot.loader.PropertiesLauncher` works – user783836 Dec 14 '16 at 17:45
  • Thanks man! You also made my day! I've struggle with this for a few hours... – Esteban Feb 25 '22 at 10:23