29

I want to make a standalone web application. I have some problems with SpringBoot.

My application is one jar file from SpringBoot.

But my application was usually needed jdbc driver jar. I want to exclude the jdbc driver jar for my application and read the library jar from the lib folder.

But SpringBoot lib folder is BOOT-INF/lib is final static. So, I want to add external classpath (lib) for the jdbc driver jar.

How to configure additional classpath in SpringBoot. Is it available?

4 Answers 4

32

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

1
  • Does this work with properties or just jars?
    – JGleason
    May 12, 2021 at 19:26
14

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

3
  • Thank yor for your answer. But it is now working. My application : /dev/myapp.jar, External library : /dev/lib/ojdbc6.jar. Set the "loader.path",but library was not loaded. Nov 15, 2016 at 2:27
  • @FIGHTINGMAMORU, your comment is confusing. It is mentioned working first and later said library was not loaded. Have you got it working?
    – Rao
    May 5, 2017 at 1:28
  • I think he meant 'not' working instead of 'now' working. I'm having a similar issue with standalone spring boot jars. It doesn't seem to be loading a single jar with loader.path. Does it have to be a directory? I don't necessarily want to load everything in the folder with my dependency jar
    – Pickles
    Jul 18, 2017 at 22:02
1

In my case, it was needed " quote to find the external lib folder on windows platform

java -cp ScoreExtractionApp.jar -Dloader.path="lib" -Dloader.main=com.sample.score.ScoreExtraction.ScoreExtractionApplication org.springframework.boot.loader.PropertiesLauncher
0

You can configure class path in maven using maven jar plugin

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
            <manifestEntries>
                <Class-Path>conf/</Class-Path>
            </manifestEntries>
       <manifest>
           <addClasspath>true</addClasspath>
           <classpathPrefix>lib/</classpathPrefix>
       </manifest>
    </archive>
   </configuration>
</plugin>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.