Java 指定主类 Spring-Boot 命令行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24322848/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:32:44  来源:igfitidea点击:

Specify main class Spring-Boot command line

javaspringspring-boot

提问by etig

I use maven plugin to set the main class like this :

我使用 maven 插件来设置主类,如下所示:

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
    <mainClass>com.myapp.main.MainClass</mainClass>
</configuration>
</plugin>

But sometimes I want run my app with another main class. What is the command line arguments to do this?

但有时我想用另一个主类运行我的应用程序。执行此操作的命令行参数是什么?

java -jar myapp-1.0.jar ...

Thx

谢谢

采纳答案by Dave Syer

There's a launcher for that in Spring Boot already. You need to build the jar with that as the Main-Class (by setting the layoutin the build config).

Spring Boot 中已经有一个启动器。您需要将 jar 构建为 Main-Class (通过layout在构建配置中设置)。

回答by Marcus A.

Following command will do the trick:

以下命令可以解决问题:

java -cp my-app.jar -Dloader.main=myApplicationClass org.springframework.boot.loader.PropertiesLauncher

回答by Andrew

Executing from Windows PowerShell I needed this format (with the quotes):

从 Windows PowerShell 执行我需要这种格式(带引号):

java -cp .\myjarfile.jar -D"loader.main=com.app.etc.FullyQualifiedMainClass" org.springframework.boot.loader.PropertiesLauncher

java -cp .\myjarfile.jar -D"loader.main=com.app.etc.FullyQualifiedMainClass" org.springframework.boot.loader.PropertiesLauncher

To clarify the accepted answer: You can directly modify the loader.main property in the jar's META-INF/MANIFEST.MF file, if you're ok with a more static solution.

澄清接受的答案:如果您对更静态的解决方案没有意见,您可以直接修改 jar 的 META-INF/MANIFEST.MF 文件中的 loader.main 属性。