You need to use PropertiesLauncher instead of JarLauncher:

java -cp "yourBoot.jar" -Dloader.path=plugins/ -Dloader.main=your.mainClass org.springframework.boot.loader.PropertiesLauncher

This will make java load the jars in the plugins directory and spring load all the classes in those jars marked as @Configuration into the context.

Answer from Camilo Sanchez on Stack Overflow
🌐
Spring
docs.spring.io › spring-boot › docs › 1.0.1.RELEASE › reference › htmlsingle
Spring Boot Reference Guide
You can use Spring Boot in the same way as any standard java library. Simply include the appropriate spring-boot-*.jar files on your classpath.
Top answer
1 of 7
15

On Linux:

Copyjava -cp MyApp.jar:/home/sleeper/thirdparty/lib -Dloader.main=myMainApplicationClass org.springframework.boot.loader.PropertiesLauncher

On Windows:

Copyjava -cp MyApp.jar;/home/sleeper/thirdparty/lib -Dloader.main=myMainApplicationClass org.springframework.boot.loader.PropertiesLauncher

This will avoid messing with the manifest or the Spring Boot Maven plugin configuration as in the other answers. It will launch your app with the PropertiesLauncher, which allows you to specify the main class in loader.main. As mentioned earlier, for some reason if you use PropertiesLauncher with loader.path, it will not add resource files to the classpath. This works around the issue by using -cp instead of -jar.

EDIT As mentioned by Pianosaurus in the comment, use ":" instead of ";" as separator in the classpath on Linux

2 of 7
14

If you just want add external libraries you can use the loader.path property.

Copyjava -Dloader.path="your-lib/" -jar your-app.jar

UPDATE

If you also need to read additional files from the classpath you have to create/change the manifest file of your application.

Lets assume that your are initializing your Spring Boot context from the class de.app.Application. Your MANIFEST.MF should looks as follows:

CopyManifest-Version: 1.0
Main-Class: de.app.Application
Class-Path: your-lib/

And the you can simply start your app with java -Dloader.path="your-lib/" -jar MyApp.jar.

For more information about the MANIFEST.MF please see Working with Manifest Files: The Basics.

🌐
Spring
docs.spring.io › spring-boot › docs › 1.1.2.RELEASE › reference › html › getting-started-first-application.html
10. Developing your first Spring Boot application
Spring Boot provides a number of “Starter POMs” that make easy to add jars to your classpath. Our sample application has already used spring-boot-starter-parent in the parent section of the POM. The spring-boot-starter-parent is a special starter that provides useful Maven defaults.
🌐
Medium
medium.com › @Jeef › dynamically-loading-libraries-into-a-springboot-application-at-run-time-80639ee5aab
Dynamically loading libraries into a SpringBoot application at run time | by Jeff Stein | Medium
October 17, 2019 - bootJar { enabled = false } jar { enabled = true } ... Additionally the application uses the PropertiesLauncher which allows one to add additional Jars to the classpath at startup.
🌐
M*A*S*H
mash213.wordpress.com › 2017 › 01 › 05 › hack-how-2-add-jars-2-springboot-classpath-with-jarlauncher
Hack – How 2 add jars 2 springboot classpath with JarLauncher ?
January 5, 2017 - java \ -cp fat_app.jar \ -Dloader.path=<path_to_your_additional_jars> \ org.springframework.boot.loader.PropertiesLauncher # It should work now, even though the main class in manifest # of fat_app.jar is set to 'org.springframework.boot.loader.JarLauncher' # and your additional classes will be picked up by springboot
🌐
Naveen Gurram
naveengurram.com › java › external-jars-with-springboot
Augment Spring boot app with external jars - Naveen Gurram
September 27, 2024 - And spring boot gives good ... time. jar -x -f application.jar \# this extracts as folders BOOT-INT META-INF com \# add the new jar to BOOT-INF/lib and add an entry to classpath.idx cp new.jar ./BOOT-INF/lib echo '- "BOOT-INF/lib/new.jar"' >> BOOT-INF/classpath.idx \# ...
🌐
Baeldung
baeldung.com › home › java › jvm › ways to add jars to classpath in java
Ways to Add JARs to Classpath in Java | Baeldung
July 19, 2025 - Java applications often depend on several bundles of external code called JARs. Here is a listing and explanation of each of the major ways to add these JARs to your application's classpath.
Find elsewhere
🌐
Medium
medium.com › @mak0024 › provide-external-jars-for-the-spring-boot-application-c65821ce6368
Provide external jars for the Spring Boot application | by AM | Medium
December 19, 2023 - -cp <path-to-executable>\executable.jar;<path-to-external-jars>\* org.springframework.boot.loader.PropertiesLauncher · An example command line for an application(server) might look like this: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:28555 -Dfile.encoding=UTF8 -XX:+HeapDumpOnOutOfMemoryError -XX:+UseG1GC -Xms512m -Xmx2000m -cp D:\Git\app.jar;D:\Git\lib\* org.springframework.boot.loader.PropertiesLauncher
🌐
Masterspringboot
masterspringboot.com › home › how to use an external jar in a spring boot application
How to use an external JAR in a Spring Boot application - Masterspringboot
December 19, 2022 - There are two simple ways to do that: the simplest one is to add the property on the command line. Also, you will not launch the Spring Boot application in the usual format (java -jar application). On the other hand, you will launch the PropertiesLauncher class but adding in the classpath your ...
🌐
LinuxTut
linuxtut.com › en › 817185c65881ec611277
[JAVA] How to add a classpath in Spring Boot
October 30, 2018 - <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <layout>ZIP</layout> </configuration> </plugin> Next, regarding the settings related to the classpath to be added, there are several setting methods. They are listed in descending order of priority. You can add the classpath with -Dloader.path at startup. ... By the way, I ran it like -Dloader.path = [path] after -jar and it didn't work, so I was worried for a while.
🌐
Spring
docs.spring.io › spring-boot › docs › 3.2.2 › reference › html › executable-jar.html
The Executable Jar Format
January 19, 2024 - Their purpose is to load resources (.class files and so on) from nested jar files or war files in directories (as opposed to those explicitly on the classpath). In the case of JarLauncher and WarLauncher, the nested paths are fixed. JarLauncher looks in BOOT-INF/lib/, and WarLauncher looks in WEB-INF/lib/ and WEB-INF/lib-provided/. You can add extra jars in those locations if you want more.
🌐
Codevomit
codevomit.xyz › bootlog › blog › how-to-provide-spring-boot-fat-jar
CodeVomit
November 26, 2016 - Manifest-Version: 1.0 Implementation-Title: bootlog Implementation-Version: 0.0.1 Archiver-Version: Plexus Archiver Built-By: merka Implementation-Vendor-Id: xyz.codevomit Spring-Boot-Version: 1.4.0.RELEASE Implementation-Vendor: CodeVomit Productions Main-Class: org.springframework.boot.loader.JarLauncher Start-Class: xyz.codevomit.bootlog.BootlogApplication Spring-Boot-Classes: BOOT-INF/classes/ Spring-Boot-Lib: BOOT-INF/lib/ Created-By: Apache Maven 3.3.3 Build-Jdk: 1.8.0_65 Implementation-URL: http://projects.spring.io/spring-boot/bootlog/ The important entries are Main-Class and Start-Class. The first is the actual main class called by the JVM. In this default case, the JarLoader is only able to locate and load classes inside BOOT-INT and JARs inside the lib directory. With this loader there's no way to add external JARs to the classpath.
🌐
Coderanch
coderanch.com › t › 480295 › java › add-jar-files-class-path
How to add new jar files to class path dynamically at runtime? (Servlets forum at Coderanch)
And if so, how can I add these uploaded JARs to the classpath for the webapp in a way that they won't be deleted next time I deploy my WAR file (ie: I can't store the uploaded JARs in the WEB-INF folder, etc.)? thanks, ... That really sounds like a job for a separate class loader. That way you could avoid all sorts of ghastly duplicate class name etc problems. Be warned, doing your own class loader is not for the faint-hearted. Runtime scanning classes for annotations indicating a plug-in architecture is what the Jersey project does.
🌐
Spring
docs.spring.io › spring-boot › specification › executable-jar › nested-jars.html
Nested JARs :: Spring Boot
example.jar | +-META-INF | +-MANIFEST.MF +-org | +-springframework | +-boot | +-loader | +-<spring boot loader classes> +-BOOT-INF +-classes | +-mycompany | +-project | +-YourClasses.class +-lib +-dependency1.jar +-dependency2.jar
🌐
Medium
medium.com › @gurram.naveen › external-jars-with-springboot-app-4be422afdd35
Augment with external jars in Spring Boot app | by Naveen Gurram | Medium
September 27, 2024 - I had a requirement to add an external jar to the spring boot after it is packaged, you might ask why can’t you package it at build time, requirement was like that after a spring boot jar is created, during runtime I want to augment it with external jar and here are some of the experiments about getting it right. Initially jar I want to add to spring boot was a plain simple java function without any external dependencies and I was able to use it to append to the classpath.
🌐
GitHub
gist.github.com › simonwoo › 0338424508496b6e171e
add new class path at runtime in java · GitHub
There are cases where we need to add classpath at runtime. For example adding jar to classpath, but depends on configuration or some logic at runtime.
Top answer
1 of 2
9

Updated answer:

You can follow the practice in my original answer, but we recently dropped this for a simpler and cleaner option that is more standard (the "Java" way). We made the change because we needed to dynamically load dependent libraries at runtime that were not available at compile time (in their exact version). In our case we wanted to load dependent jars only from separate folder(s) and not from an executable jar. We ended up having duplicate dependencies in the executable jars and in separate folder(s), so we decided to drop the executable jar Properties Launcher and instead only load dependencies from separate folders. This is often NOT the best option and should be evaluated for your use case. I prefer reading the standard Java classpath.

To run a Spring Boot app without an executable jar, we used Maven Assembly to put the dependent jars in a /libs directory and dropped the spring-boot-maven-plugin. The steps and some code for this are below:

  1. Remove the spring-boot-maven-plugin that creates the executable jar in ZIP format
  2. Add the following to your assembly XML

    <dependencySets> <dependencySet> <outputDirectory>where you want the libs to go</outputDirectory> <useProjectArtifact>whether you want to include the project artifact here</useProjectArtifact> </dependencySet> </dependencySets>

  3. Run your code from the main class and include the dependent jar folder(s) on the classpath. Use the standard classpath notation on your OS and not the custom, awkward PropertiesLauncher loader path syntax

    java -cp <standard-classpath> <main-class>

An example of an actual call: java -cp $CLASSPATH:./lib/*:./cfg/*:my-app.jar Application.class

In this way you execute the Spring Boot app via standard java execution call, no custom Spring loading syntax. You just need to ensure that all of your dependencies are available on the classpath at runtime. We found this much easier to maintain and made this the standard for all of our apps.

Original answer:

After some researching, and thanks to @TuyenNguyen's helpful answer I was able to get the following working:

I added the following to my spring-boot-maven-plugin so that when I run from the command line it uses the PropertiesLauncher instead of the JarLauncher:

<configuration>
     <mainClass>${mainClass}</mainClass>
     <layout>ZIP</layout> //THIS IS THE IMPORTANT PART
</configuration>

See here and here for more about the PropertiesLauncher options. It allows you to set the classpath, among other things. See here, here, and here for where I found the answer to this problem. Using format ZIP makes the PropertiesLauncher be used.

From there, I was able to use this command to launch the application as I intended:

java -Dloader.path=../config,../ -Dloader.config.location=classpath:application.properties -jar ../app-exec.jar

Another important note: when specifying the -Dloader.path make sure to use comma-separated values and only directories and files, as described here. Also, be sure to put the -D args before you specify -jar jar or they will not be set.

If anyone has any suggestions or edits to further improve this answer or the original question in order to help additional users, please let me know or make the edits yourself!

2 of 2
2

If you don't put your files in src/main/resources then you can put it inside any folder that you want, BUT you must set your folder as a resources folder. Because classpath is always point to resources folder. Once you make your folder as a resource folder, it will be packaged into the jar. If you want to edit your resource file, just using 7 zip tool to open your jar -> edit files -> save -> it will update your change in the jar.

Another solution is create a folder, put all files you want to edit and not packaged in that, then set classpath manually to that folder every time you run, but the way you set above is not correct, try this solution for set classpath correct way.