How to Dynamically Load External JARs into Spring Boot at Runtime
This article explains a step‑by‑step approach to load external JAR files into the JVM, configure Spring Boot to recognize the new classes, and choose among classpath expansion, Spring Boot's loader.path, or a custom ClassLoader, complete with Maven settings and code examples.
Implementation Plan
The solution consists of two steps: load the external class into the JVM and let Spring scan it.
Load Class into JVM
Assume the JAR to be loaded is placed in /user/local/java/plugins. Three implementation methods are available:
Expand the -cp of the AppClassLoader to load external JARs.
Use Spring Boot’s loader.path startup parameter.
Define a custom ClassLoader to load JARs from a fixed directory or an environment variable.
Methods 1 and 3 rely on JDK mechanisms; method 2 is a Spring Boot extension.
Solution 1 – Expand -cp
Specify the classpath with the classpath option, but it only works when the JAR is started via its Main method. The Spring Boot manifest shows Main-Class: org.springframework.boot.loader.JarLauncher, so the launch command must be changed to:
java -cp /user/local/java/plugins org.springframework.boot.loader.JarLauncherAdvantage: simple implementation.
Disadvantage: identical launch command for every JAR, making differentiation difficult.
Solution 2 – Spring Boot loader.path
When the application is packaged as an executable JAR, the -jar launch disables -cp. Adding loader.path allows additional JARs to be loaded, but the Main-Class must be set to PropertiesLauncher in the Maven plugin configuration:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>false</includeSystemScope>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>Test</finalName>
</build>The layout can be JAR, WAR, ZIP, MODULE, or NONE, each affecting the launcher class.
Solution 3 – Custom ClassLoader
Invoke a custom class loader at a specific program point to load JARs from a designated directory. Example code:
// custom class loading
SpringApplication.run(SpringBootDemo.class, args);Solution 4 – JVM Options
Modifying -Xbootclasspath/a or java.ext.dirs changes the system class loader but is unsafe and not recommended.
Scanning Loaded Classes into Spring
If business JARs share a common package prefix, configure scanBasePackages in @SpringBootApplication to include that prefix, e.g., com.demo. For packages that do not match, add the appropriate entries to META-INF/spring.factories for automatic configuration.
# Auto Configure
org.springframework.boot.env.EnvironmentPostProcessor=\
com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.baomidou.mybatisplus.autoconfigure.IdentifierGeneratorAutoConfiguration,\
com.baomidou.mybatisplus.autoconfigure.MybatisPlusLanguageDriverAutoConfiguration,\
com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfigurationSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
