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!

Answer from wrslatz on Stack Overflow
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 › 1.0.1.RELEASE › reference › htmlsingle
Spring Boot Reference Guide
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.0.1.RELEASE</version> </parent> <!-- Additional lines to be added here... --> </project> This should give you a working build, you can test it out by running mvn package (you can ignore the “jar will be empty - no content was marked for inclusion!” warning for now). Spring Boot provides a number of “Starter POMs” that make easy to add jars to your classpath.
🌐
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
<?xml version="1.0" encoding="UTF-8"?> ... lines to be added here... --> </project> This should give you a working build, you can test it out by running mvn package (you can ignore the “jar will be empty - no content was marked for inclusion!” warning for now). Spring Boot provides a number of “Starter POMs” that make easy to add jars to your classpath...
🌐
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. The java command must be specified in the orderjava [-options] -jar jarFile [args ...].
🌐
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.
🌐
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 - We had a scenario where we were using the JarLauncher of springboot but had to add additional jars to classpath. i.e. we would launch the springboot app using the following command java -jar fat_app.jar Since springboot when using JarLaunhcer , ignores -classpath or -cp argument of java, our attempt to add a jar via -cp…
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.

🌐
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 - To set the classpath via the command line, we use the -classpath option when running the java command:
Find elsewhere
🌐
Spring
docs.spring.io › spring-boot › docs › 1.0.1.RELEASE › reference › html › boot-features-external-config.html
21. Externalized Configuration
By default SpringApplication will convert any command line option arguments (starting with “--”, e.g. --server.port=9000) to a property and add it to the Spring Environment.
🌐
GitHub
github.com › spring-projects-experimental › spring-boot-thin-launcher › issues › 78
Ability to add additional external classpath directory · Issue #78 · spring-projects-experimental/spring-boot-thin-launcher
November 23, 2018 - spring-boot-thin-launch works great for my none spring app as well but I lose the ability the setup external classpath. Possible to add -Dthin.extraClassPath=xxxx ? This option allows me to configure my logback.xml location
Author   spring-projects-experimental
🌐
Stack Overflow
stackoverflow.com › questions › 70410549 › classpath-file-directory-root-classpath-in-spring-boot-project › 70411646
java - Classpath file directory root (.classpath) in Spring-Boot project - Stack Overflow
I want to add a dependency manually inside .classpath file of my Spring-Boot project, but I can't find that file. Could you please tell me where the route is? This is my project structure:
🌐
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 ... 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. By default, if YAML is used, then files with the ‘.yml’ extension are also added to the ...
🌐
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 ...
🌐
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.
🌐
Spring
docs.spring.io › spring-boot › docs › 2.7.13 › reference › html › cli.html
Spring Boot CLI
$ spring help run spring run - Run a spring groovy script usage: spring run [options] <files> [--] [args] Option Description ------ ----------- --autoconfigure [Boolean] Add autoconfigure compiler transformations (default: true) --classpath, -cp Additional classpath entries --no-guess-dependencies ...