Java 带有类路径的 Spring Boot 可执行 Jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39716796/
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
Spring Boot Executable Jar with Classpath
提问by user1670498
I am building a software system to interact with an enterprise software system, using Spring Boot. My system depends on some jars and *.ini files from that enterprise system, so I cannot pack all dependencies in Maven. I would like to be able to run Spring Boot as Executable Jar with embedded Tomcat. I would also like to be able to set the classpath via the command line. So something like:
我正在构建一个软件系统来与企业软件系统进行交互,使用 Spring Boot。我的系统依赖于该企业系统中的一些 jars 和 *.ini 文件,因此我无法在 Maven 中打包所有依赖项。我希望能够将 Spring Boot 作为带有嵌入式 Tomcat 的 Executable Jar 运行。我还希望能够通过命令行设置类路径。所以像:
java -classpath /home/sleeper/thirdparty/lib -jar MyApp.jar
However, -classpath and -jar cannot co-exist. I have tried "-Dloader.path". It was able to load all the jar files under the folder, but not other things, like *.ini files in the folder.
但是,-classpath 和-jar 不能共存。我试过“-Dloader.path”。它能够加载文件夹下的所有 jar 文件,但不能加载其他内容,例如文件夹中的 *.ini 文件。
So is there a way we can make -classpath to work with an Spring executable jar with embedded Tomcat?
那么有没有办法让 -classpath 与带有嵌入式 Tomcat 的 Spring 可执行 jar 一起使用?
Thank you in advance for all the help.
预先感谢您提供的所有帮助。
采纳答案by Paul Wasilewski
If you just want add external libraries you can use the loader.path
property.
如果您只想添加外部库,则可以使用该loader.path
属性。
java -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:
让我们假设您正在从 class 初始化您的 Spring Boot 上下文de.app.Application
。您MANIFEST.MF
应该如下所示:
Manifest-Version: 1.0
Main-Class: de.app.Application
Class-Path: /home/sleeper/thirdparty/lib/
And the you can simply start your app with java -jar MyApp.jar
.
您可以简单地使用java -jar MyApp.jar
.
For more information about the MANIFEST.MF please see Working with Manifest Files: The Basics.
有关 MANIFEST.MF 的更多信息,请参阅使用清单文件:基础知识。
回答by Peter Tarlos
You mentioned that you needed to load *.ini files from an external folder. I had to do something similar, load CSV files from an external folder.
您提到您需要从外部文件夹加载 *.ini 文件。我不得不做类似的事情,从外部文件夹加载 CSV 文件。
My file structure looked like this
我的文件结构看起来像这样
./myapp.jar
./config/file.csv
I was using the ResouceLoader to load the files as:
我使用 ResouceLoader 将文件加载为:
Resource res = resourceLoader.getResource("classpath:file.csv");
File csvFile = res.getFile();
Start script:
启动脚本:
java -Dloader.path="config" -jar your-app.jar
The resource was not loading from the "config" folder as expected. After some research I found out that I had to change my Maven plugin configuration to use ZIP layout.
资源未按预期从“config”文件夹加载。经过一番研究,我发现我必须更改我的 Maven 插件配置才能使用 ZIP 布局。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layout>ZIP</layout>
</configuration>
</plugin>
This will direct Spring Boot to use PropertiesLauncher, which allows loading external resources from "loader.path".
这将指示 Spring Boot 使用 PropertiesLauncher,它允许从“loader.path”加载外部资源。
See this excellent articlefor more detail.
有关更多详细信息,请参阅这篇出色的文章。
回答by Mihail Kostira
On Linux:
在 Linux 上:
java -cp MyApp.jar:/home/sleeper/thirdparty/lib -Dloader.main=myMainApplicationClass org.springframework.boot.loader.PropertiesLauncher
On Windows:
在 Windows 上:
java -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.
这将避免在其他答案中弄乱清单或 Spring Boot Maven 插件配置。它将使用 PropertiesLauncher 启动您的应用程序,它允许您在 loader.main 中指定主类。如前所述,出于某种原因,如果您将 PropertiesLauncher 与 loader.path 一起使用,它不会将资源文件添加到类路径中。这可以通过使用 -cp 而不是 -jar 来解决这个问题。
EDIT As mentioned by Pianosaurus in the comment, use ":" instead of ";" as separator in the classpath on Linux
编辑正如 Pianosaurus 在评论中提到的,使用“:”而不是“;” 作为 Linux 类路径中的分隔符
回答by vaquar khan
java -cp C:\jar-path\your-jar-1.2.0.jar -Dloader.main=package-and-main class -Dloader.path=external dependency jar path org.springframework.boot.loader.PropertiesLauncher -Dspring.profiles.active=profile etc -default,test --spring.config.location=external properties file name
If want to define external memory use
如果要定义外部存储器使用
java -ms8g -mx8g -cp
java -cp
java -cp
-Dloader.main
-Dloader.main
Spring Boot's org.springframework.boot.loader.PropertiesLauncher comes with a JVM argument to let you override the logical main-class called loader.main:
Spring Boot 的 org.springframework.boot.loader.PropertiesLauncher 带有一个 JVM 参数,让你覆盖名为 loader.main 的逻辑主类:
-Dloader.path
-Dloader.path
Tell the PropertiesLauncher that it should pick up any libraries found in the “lib”
告诉 PropertiesLauncher 它应该选择在“lib”中找到的任何库
org.springframework.boot.loader.PropertiesLauncher
org.springframework.boot.loader.PropertiesLauncher
Spring Boot's org.springframework.boot.loader.PropertiesLauncher comes with a JVM argument to let you override the logical main-class called loader.main:
Spring Boot 的 org.springframework.boot.loader.PropertiesLauncher 带有一个 JVM 参数,让你覆盖名为 loader.main 的逻辑主类:
java -cp bootApp.jar -Dloader.main=org.khan.DemoApplication org.springframework.boot.loader.PropertiesLauncher
-Dspring.profiles.active
-Dspring.profiles.active
If you are using Spring profile then you need to set profile first
如果您使用的是 Spring 配置文件,那么您需要先设置配置文件
set SPRING_PROFILES_ACTIVE=default,test
or window run type envi and add
或窗口运行类型env并添加
spring_profiles_active
default,test
--spring.config.location
--spring.config.location
Directory is specified then that is where the application.properties is searched for
指定目录然后搜索 application.properties