Spring Boot Executable Jar with Classpath
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
java -cp C:\jar-path\your-jar-1.2.0.jar -Dloader.main=package-and-main class -Dloader.path=external dependency jar path org.springframework.boot.loader.PropertiesLauncher -Dspring.profiles.active=profile etc -default,test --spring.config.location=external properties file name
If want to define external memory use
java -ms8g -mx8g -cp
java -cp
- Differences between "java -cp" and "java -jar"?
-Dloader.main
Spring Boot’s org.springframework.boot.loader.PropertiesLauncher comes with a JVM argument to let you override the logical main-class called loader.main:
-Dloader.path
Tell the PropertiesLauncher that it should pick up any libraries found in the “lib”
org.springframework.boot.loader.PropertiesLauncher
Spring Boot’s org.springframework.boot.loader.PropertiesLauncher comes with a JVM argument to let you override the logical main-class called loader.main:
java -cp bootApp.jar -Dloader.main=org.khan.DemoApplication org.springframework.boot.loader.PropertiesLauncher
-Dspring.profiles.active
If you are using Spring profile then you need to set profile first
set SPRING_PROFILES_ACTIVE=default,test
or window run type envi and add
spring_profiles_active
default,test
--spring.config.location
Directory is specified then that is where the application.properties is searched for
- https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
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.
You mentioned that you needed to load *.ini files from an external folder. I had to do something similar, load CSV files from an external folder.
My file structure looked like this
./myapp.jar
./config/file.csv
I was using the ResouceLoader to load the files as:
Resource res = resourceLoader.getResource("classpath:file.csv");
File csvFile = res.getFile();
Start script:
java -Dloader.path="config" -jar your-app.jar
The resource was not loading from the "config" folder as expected. After some research I found out that I had to change my Maven plugin configuration to use ZIP layout.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
</configuration>
</plugin>
This will direct Spring Boot to use PropertiesLauncher, which allows loading external resources from "loader.path".
See this excellent article for more detail.