You can use the loader.path parameter to define a location for an external lib folder. All jars under this folder will be added to the classpath. For example, if you'd like to define C:\extLib as your external lib folder, you can do the following:

java -Dloader.path=/C:/extLib/ -jar aapName.jar

For this to work, you need to use the PropertiesLauncher. There are two ways to do that:

Option 1

Update the project pom.xml and add the following tag:

<configuration>  <!-- added -->
  <layout>ZIP</layout> <!-- to use PropertiesLauncher -->
</configuration>

Effective build tag, the post-update looks like below:

<build> 
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>  <!-- added -->
                <layout>ZIP</layout> <!-- to use PropertiesLauncher -->
            </configuration>
        </plugin>
    </plugins>
</build>

Option 2

Use the PropertiesLauncher when launching the application from the commandline:

java -cp aapName.jar -Dloader.path=/C:/extLib/ org.springframework.boot.loader.PropertiesLauncher

References:
How to add jars to SpringBoot classpath with jarlauncher

Answer from giftednewbie on Stack Overflow
🌐
Spring
docs.spring.io › spring-boot › docs › 1.0.1.RELEASE › reference › htmlsingle
Spring Boot Reference Guide
In summary: there is a spring script (spring.bat for Windows) in a bin/ directory in the .zip file, or alternatively you can use java -jar with the .jar file (the script helps you to be sure that the classpath is set correctly). GVM (the Groovy Environment Manager) can be used for managing multiple versions of various Groovy and Java binary packages, including Groovy itself and the Spring Boot CLI.
Discussions

java - How to set Spring Classpath - Stack Overflow
I am using IntelliJ and jus checked out working code from the svn.I am struggling to run the jar. Its a simple core java Spring project. Since I get the above error.I understand that the spring p... More on stackoverflow.com
🌐 stackoverflow.com
java - Spring Boot Executable Jar with Classpath - Stack Overflow
My system depends on some jars and *.ini files from that enterprise system, so I cannot pack all dependencies in Maven. I would like to be able to run Spring Boot as Executable Jar with embedded Tomcat. I would also like to be able to set the classpath via the command line. More on stackoverflow.com
🌐 stackoverflow.com
spring boot - How to add classpath resource in application.yml file - Stack Overflow
With spring-boot, in case of application.properties file there is provision to specify classpath in value. app.templates = classpath:app\mock-templates How to specify same thing inside applicatio... More on stackoverflow.com
🌐 stackoverflow.com
java - Spring Boot add files to classpath from command line execution - Stack Overflow
Another solution is create a folder, ... 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. ... thanks, the second option you provided looks like the appropriate solution. However, given that this is a repackaged Spring Boot app, I don't ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › @kouomeukevin › demystifying-the-classpath-in-spring-boot-why-and-how-to-use-it-c76ec31d8986
Demystifying the Classpath in Spring Boot: Why and How to Use it | by Kevin Kouomeu | Medium
April 6, 2024 - In this guide, we’ll talk about the Spring Boot classpath and give you the knowledge you need to navigate it with confidence.
🌐
LinuxTut
linuxtut.com › en › 817185c65881ec611277
[JAVA] How to add a classpath in Spring Boot
October 30, 2018 - You can add a classpath by using PropertiesLauncher as the Main class. If you are using Spring Boot Maven Plugin, you can use Properties Launcher by setting layout to ZIP or DIR.
🌐
CodingTechRoom
codingtechroom.com › question › spring-boot-classpath-configuration
How to Configure the Classpath in Spring Boot Applications? - CodingTechRoom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> Configuring the classpath in a Spring Boot application is crucial for effective application execution and resource management. The classpath determines which libraries and resources the Java Virtual Machine (JVM) can access during runtime.
🌐
IQCode
iqcode.com › code › java › how-to-add-classpath-in-spring-boot
how to add classpath in spring boot Code Example
August 27, 2021 - java -cp aapName.jar -Dloader.path=/C:/extLib/ org.springframework.boot.loader.PropertiesLauncher-
Find elsewhere
🌐
1kevinson
1kevinson.com › demystifying-the-classpath-in-spring-boot
Demystifying the Classpath in Spring Boot: Your Guide to Class Loading
January 9, 2025 - By establishing a defined classpath, the application ensures that all its components are accessible and functional. Spring Boot takes a step further by simplifying the classpath management process. It employs conventions for organizing resources and automatically scans classpath locations, eliminating the need for manual configuration.
🌐
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
The @RequestMapping annotation provides “routing” information. It is telling Spring that any HTTP request with the path "/" should be mapped to the home method. The @RestController annotation tells Spring to render the resulting string directly back to the caller. The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360008117659-Run-a-spring-boot-application-with-test-resources-in-classpath
Run a spring boot application with test resources in classpath – IDEs Support (IntelliJ Platform) | JetBrains
And you can use this `--spring.config.location` in Program arguments of Run Configuration: <https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config> We do not recommend to change classpath at all, but still in case you are sure you need that, you may use one of the following methods at your own risk:
🌐
Baeldung
baeldung.com › home › java › core java › understanding java’s classpath vs. build path
Understanding Java’s Classpath vs. Build Path | Baeldung
August 28, 2024 - The classpath is an environment variable used by the Java Virtual Machine (JVM) to locate and load classes when running a Java program. It specifies a list of directories, JAR files, and ZIP files where the JVM should look to find and load class files. We can set the classpath from the command line or in an integrated development environment (IDE).
Top answer
1 of 7
15

On Linux:

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

On Windows:

java -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.

java -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:

Manifest-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.

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.

🌐
Stack Overflow
stackoverflow.com › questions › 41463070 › how-to-specify-additional-classpath-in-command-line-when-running-spring-boot-app
How to specify additional classpath in command line when running Spring Boot application via Maven? - Stack Overflow
April 1, 2017 - Try setting folders in Spring Boot Maven Plugin in the POM. But why do you want to do this to begin with? ... That's just how this project is set up. It's a Tomcat web application and usually a config directory is added during deployment. I need to use a few different configurations, and don't want to be copying files around on my local machine if I can just change a command-line argument instead. When I run it from IntelliJ, I can add a runtime classpath in the IntelliJ project settings, which works fine.
🌐
Reddit
reddit.com › r/springboot › how to add an arbitrary additional classpath to spring boot
r/SpringBoot on Reddit: How to add an arbitrary additional classpath to spring boot
1 month ago -

I want to be able to reference external classpath resources via SomeClass.class.getResourceAsStream(). These resources live OUTSIDE the jar on the filesystem. I've tried many ways to do this with command line changes using -cp, etc. but I must be doing something wrong. My production application needs to be able to pick up files from the local filesystem, but there are some classes out of my control that expect these files/resources to be on the CLASSPATH and picking them up using my methodology above. These files and resources CANNOT be inside the jar. I'm using a quite old version of spring boot 2.4.2. Any guidance would be greatly appreciated.

🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 737994 › springboot-application-path
SpringBoot application path. - Microsoft Q&A
February 19, 2022 - <environmentVariables> <environmentVariable name="CLASSPATH" value="/home/site/wwwroot/keys;%CLASSPATH%" /> </environmentVariables>