16

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
piet.t
  • 11,718
  • 21
  • 43
  • 52
Carlos Alberto
  • 7,761
  • 13
  • 52
  • 72

3 Answers3

9

See the documentation about PropertiesLauncher:

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)

Chris White
  • 29,949
  • 4
  • 71
  • 93
  • I still can't make it work with loader.path, it seems to ignore whatever I put in there. Could you add an example, please? Thanks. – Pedro Lopez Jan 22 '17 at 22:17
  • 1
    Never mind, it turns out that you need to build your jar with ZIP layout for the loader.path to work. – Pedro Lopez Jan 22 '17 at 23:02
  • Use as below: 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 – Sandeep Kumar Dec 31 '20 at 09:24
1

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

Sandeep Kumar
  • 596
  • 5
  • 7
-2

Isn't that what the classpath is for? As long as the jdbc driver jars are on the classpath, this should work. Something like this:

java -classpath /path/to/driver/jar -jar application.jar

You could also set the CLASSPATH environment variable for the same.

Jigish
  • 1,764
  • 1
  • 15
  • 20
  • 3
    Not with spring-boot and its executable-jar format, and besides you traditionally can't mix the jar and classpath options - http://stackoverflow.com/questions/15930782/call-java-jar-myfile-jar-with-additional-classpath-option – Chris White Jan 21 '16 at 23:56
  • java cmd ignore "classpath" options with jar option. – xyzlast Aug 23 '17 at 01:15