Java 如何在 SpringBoot 中配置额外的类路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40499548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure additional classpath in SpringBoot?
提问by fightingmamoru
I want to make a standalone web application. I have some problems with SpringBoot.
我想制作一个独立的 Web 应用程序。我在使用 SpringBoot 时遇到了一些问题。
My application is one jar file from SpringBoot.
我的应用程序是来自 SpringBoot 的一个 jar 文件。
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.
但是我的应用程序通常需要 jdbc 驱动程序 jar。我想为我的应用程序排除 jdbc 驱动程序 jar 并从 lib 文件夹中读取库 jar。
But SpringBoot lib folder is BOOT-INF/libis final static. So, I want to add external classpath (lib) for the jdbc driver jar.
但是 SpringBoot lib 文件夹BOOT-INF/lib是final static. 所以,我想为 jdbc 驱动程序 jar 添加外部类路径(lib)。
How to configure additional classpath in SpringBoot. Is it available?
如何在 SpringBoot 中配置额外的类路径。是可用的么?
回答by mhasan
You may refer this below link from spring boot:
您可以从 spring boot 中参考以下链接:
You can use the loader.path property to define a lib folder location
您可以使用 loader.path 属性来定义 lib 文件夹位置
回答by giftednewbie
You can use the loader.pathparameter 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:\extLibas your external lib folder, you can do the following:
您可以使用该loader.path参数来定义外部 lib 文件夹的位置。此文件夹下的所有 jar 文件都将添加到类路径中。例如,如果您想定义C:\extLib为外部 lib 文件夹,您可以执行以下操作:
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:
为此,您需要使用 PropertiesLauncher。有两种方法可以做到这一点:
Option 1
选项1
Update the project pom.xml and add the following tag:
更新项目 pom.xml 并添加以下标记:
<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
选项 2
Use the PropertiesLauncher when launching the application from the commandline:
从命令行启动应用程序时使用 PropertiesLauncher:
java -cp aapName.jar -Dloader.path=/C:/extLib/ org.springframework.boot.loader.PropertiesLauncher
References:
How to add jars to SpringBoot classpath with jarlauncher
回答by Ahmed Salem
In my case, it was needed " quote to find the external lib folder on windows platform
在我的情况下,需要“引用才能在 Windows 平台上找到外部 lib 文件夹
java -cp ScoreExtractionApp.jar -Dloader.path="lib" -Dloader.main=com.sample.score.ScoreExtraction.ScoreExtractionApplication org.springframework.boot.loader.PropertiesLauncher

