Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External library folder for Spring Boot

I wonder how to externalize all jdbc drivers for my Spring Boot applications, I would not like to insert jdbc drivers into my fat jar once the application is built.

Is there any way to set a java vm parameter, informing which external folder should be included with jar execution? Or may else exists some kind of spring property for it.

For example:

java  -DLib=file:\\\c:\Drivers -jar sample.jar
like image 583
Carlos Alberto Avatar asked Jun 10 '15 00:06

Carlos Alberto


People also ask

How do I add an external library to spring boot?

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 Spring Boot application.


2 Answers

See the documentation about PropertiesLauncher:

  • https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar-property-launcher-features

Looks like you can use the loader.path property to define a lib folder location, containing jars to load - in fact the lib folder (inline with the jar) is the default location:

loader.path (if empty) defaults to lib (meaning a local directory or a nested one if running from an archive)

like image 192
Chris White Avatar answered Oct 22 '22 04:10

Chris White


CLASSPATH and -classpath will not be working in case of running Spring boot jar file. So there are below options are available:

  • Use -Dloader.path e.g. java -cp MyOwn.jar -Dloader.path=C:\Sandeep\lib -Dconsole.level=INFO -Dloader.main=abc.Main org.springframework.boot.loader.PropertiesLauncher --spring.config.name=application

  • Another option to place other jars into the JRE/lib/ext directory

  • Third option, open Spring Boot Jar with WinRAR application and add the jar file into the Spring Boot jar

like image 1
Sandeep Kumar Avatar answered Oct 22 '22 06:10

Sandeep Kumar