最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Spring boot 3 fat jar doesnt scan libs or config directory - Stack Overflow

programmeradmin1浏览0评论

I have a spring boot 3 project that is packaged as a jar file with all the dependencies in it (fat jar). When I run the jar file using the command java -jar MyApp.jar then the application starts so that works. I want to run the jar file, with plugin possibility by adding extra jars in production environments.

From what I understand is that when you run the jar file the directories lib and config are added to the classpath. These folders being subfolders of the folder where the jar file is located.

MyApp.jar
| -lib
| -config

I have done this and yet the application.property file in the config folder is not detected and extra jars in the lib folder aren't loaded either.

Is it possible to solve this and if so how?

I have a spring boot 3 project that is packaged as a jar file with all the dependencies in it (fat jar). When I run the jar file using the command java -jar MyApp.jar then the application starts so that works. I want to run the jar file, with plugin possibility by adding extra jars in production environments.

From what I understand is that when you run the jar file the directories lib and config are added to the classpath. These folders being subfolders of the folder where the jar file is located.

MyApp.jar
| -lib
| -config

I have done this and yet the application.property file in the config folder is not detected and extra jars in the lib folder aren't loaded either.

Is it possible to solve this and if so how?

Share Improve this question asked Mar 13 at 15:37 Martijn HiemstraMartijn Hiemstra 31 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

To run a Spring Boot 3 fat JAR with external libraries in a lib directory and configuration files in a config directory, follow these steps:

  1. Directory Structure: Ensure your structure looks like this:

    enter image description here

  2. Run the Application: Use the following command to start your application, ensuring to include the external libraries and configuration folder:

    java -cp "MyApp.jar:lib/*" .springframework.boot.loader.WarLauncher --spring.config.additional-location=file:./config/

    Explanation:

    • -cp "MyApp.jar:lib/*": Sets the classpath to include your JAR and all JARs in the lib folder.

    • --spring.config.additional-location=file:./config/: Tells Spring to load configuration files from the config directory.

    Make sure to adjust the path separator if you're on Windows (; instead of :).

To achieve this, you need to ensure that the config directory is added to the classpath and that the lib directory is included for additional JARs. Here is how you can do it:

java -cp "MyApp.jar:lib/*" -Dspring.config.location=./config/application.properties .springframework.boot.loader.JarLauncher
发布评论

评论列表(0)

  1. 暂无评论