/src/main/resources is a Maven project structure convention. It's a path inside your project where you place resources. During the build step, Maven will take files in there and place them in the appropriate place for you to use them in your runtime classpath, eg in an executable .jar, some physical file system location used in the classpath (with java's -cp option), etc.

I could choose to build my application myself or with a different build tool. In such a case, /src/main/resources would not exist. However, the intention is for the classpath to be the same, ie. to contain the same resources and .class files.

The Spring boot documentation talks about the classpath because it shouldn't make assumptions about how your project is set up.

Answer from Sotirios Delimanolis 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.
🌐
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.
🌐
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-
🌐
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.
🌐
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.
Find elsewhere
🌐
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:
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.

🌐
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).
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 206406069-Gradle-Spring-Boot-classpath-issues
Gradle + Spring Boot classpath issues – IDEs Support (IntelliJ Platform) | JetBrains
the classpath to use for running the actual Spring Boot application can be set in the run configuration dialog via combobox "Use classpath of module".
🌐
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.
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.

🌐
Spring
docs.spring.io › spring-boot › docs › 2.1.2.RELEASE › reference › html › howto-properties-and-configuration.html
76. Properties and Configuration
spring.config.location (SPRING_CONFIG_LOCATION): The file to load (such as a classpath resource or a URL). A separate Environment property source is set up for this document and it can be overridden by system properties, environment variables, or the command line. No matter what you set in the environment, Spring Boot always loads application.properties as described above.
🌐
Gradle
discuss.gradle.org › help/discuss
Adding a resource directory to the runtime class path and excluding it from the jar - Help/Discuss - Gradle Forums
December 12, 2024 - Greetings. I have a Java/Spring Boot project where I need to decrypt some sops encrypted .env files when the app starts up. The encrypted files are checked into the BitBucket repo, and I need to 1) use the Java class path to find the appropriate file, 2) decrypt it when the app starts using a fixed directory structure/naming convention, and 3) make sure that neither the encrypted or decrypted files are part of the Jar file that gets built in the project.
🌐
Jaspersoft Community
community.jaspersoft.com › forum
How to set net.sf.jasperreports.compiler.classpath in a Spring Boot uber JAR? - Developers and API - Jaspersoft Community
April 19, 2025 - Hi, could you please help me figure out the correct way to set the net.sf.jasperreports.compiler.classpath environment variable in Spring Boot? I have an uber JAR, which contains a bunch of libraries inside BOOT-INF/lib/. I need to set the net.sf.jasperreports.compiler.classpath variable correctl...