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
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
You may refer this below link from spring boot:
https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar-property-launcher-features
You can use the loader.path property to define a lib folder location
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
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.
Given that you are dealing with a read-only filesystem, using Spring Boot’s PropertiesLauncher may be your best option.
With the properties launcher you can use the loader.path system property to configure the classpath. BOOT-INF/classes and jars in BOOT-INF/lib are always included so loader.path should just point to the additional volume-mounted jar file.
You can configure Spring Boot to build a jar that uses properties launcher with Gradle and Maven.
For me this works with java 17:
java --patch-module java.base=classes app.jar
with the classes directory containing my property files.
You need to use PropertiesLauncher instead of JarLauncher:
java -cp "yourBoot.jar" -Dloader.path=plugins/ -Dloader.main=your.mainClass org.springframework.boot.loader.PropertiesLauncher
This will make java load the jars in the plugins directory and spring load all the classes in those jars marked as @Configuration into the context.
I believe "standard" java approach should work here, e.g. for Linux:
java -cp "yourBoot.jar:plugins/*" org.springframework.boot.loader.JarLauncher
Only trick is that I have taken Main-Class from boot's uberjar.
If you're using maven to build your application, it runs in the netbeans IDE, but you'll need to define a plugin in your maven pom.xml in order to include all of the required .jars in your final .jar to make it executable.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>yourmainpackagename.YourMainClass</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
This approach uses the maven shade plugin. You could also be using the maven assembly plugin, be prepared to face nasty bugs though :p
cheers.
If you are compiling using Netbeans IDE (with proper build path) then build artifact (the jar) and the lib/ would be in dist directory.
cd into the ${project-root}/dist directory and then run :
java -jar XXXXX.jar
The dependency on the libraries must be already taken care of in MANIFEST by Netbeans.
PS: See the generated README in the dist directory.