How to Dynamically Load External JARs into SpringBoot and Scan Them
This guide explains how to load external JAR files into the JVM, configure SpringBoot to recognize them, and choose among several class‑loading strategies—including expanding the classpath, using the loader.path parameter, or implementing a custom classloader—while providing Maven configuration examples and code snippets.
Implementation Plan
To load a class into the JVM and let Spring scan it, complete two steps: make the class available to the JVM and ensure Spring discovers it.
Load the class into the JVM.
Configure Spring to scan the loaded class.
Loading the Class into the JVM
The target JAR is located in /user/local/java/plugins. Three implementation options are available:
Expand the -cp option so that the AppClassLoader loads external JARs.
Use SpringBoot’s startup parameter loader.path to load additional JARs.
Define a custom classloader to load JARs from a fixed directory or an environment‑defined path.
Option 1: Expand -cp via AppClassLoader
Specify the classpath with classpath. This works when the JAR is started via its Main method.
java -cp /user/local/java/plugins org.springframework.boot.loader.JarLauncherOption 2: SpringBoot loader.path Parameter
When the application is packaged as an executable JAR, use java -jar together with loader.path to add external JARs. The loader.path parameter is effective only when the Main-Class is set to PropertiesLauncher in the Maven build.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>false</includeSystemScope>
<layout>ZIP</layout>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin>
</plugins>
</build>The layout element can be set to one of the following values, each defining a different Main-Class:
JAR : Main-Class: org.springframework.boot.loader.JarLauncher WAR : Main-Class: org.springframework.boot.loader.WarLauncher ZIP :
Main-Class: org.springframework.boot.loader.PropertiesLauncherMODULE : packages all dependencies except those with provided scope, without a launcher.
NONE : packages all dependencies and excludes any SpringBoot launcher.
When the Main-Class is JarLauncher or PropertiesLauncher, the JARs referenced by loader.path and those under BOOT-INF/lib are loaded by org.springframework.boot.loader.LaunchedURLClassLoader.
Note: PropertiesLauncher starts noticeably slower than JarLauncher .
Option 3: Custom Classloader
Invoke a custom classloader at a specific program point to load JARs from a designated directory. This offers high customisation but requires careful timing.
// Example of invoking a custom classloader
SpringApplication.run(SpringBootDemoApplication.class, args);Option 4: JVM Boot Classpath or Extension ClassLoader
Modify the system classloader using -Xbootclasspath/a or java.ext.dirs. This approach is unsafe and not recommended.
Scanning Loaded Classes into Spring
Declare the base package for component scanning so that any classes under com.demo are detected:
@SpringBootApplication(scanBasePackages = {"com.demo"})
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}If the JAR does not follow the package‑prefix convention, add the required auto‑configuration classes to META-INF/spring.factories, for example for MyBatis‑Plus:
# 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.MybatisPlusAutoConfigurationWith these configurations, external JARs can be loaded at runtime and their beans will be managed by SpringBoot.
Signed-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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
