Boot应用程序的非主类

Boot应用程序的非主类

本文介绍了通过命令行运行Spring Boot应用程序的非主类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spring Boot应用程序打包为可执行jar.应用程序有一个实用程序类,该类具有要通过命令行调用的main方法,但似乎命令无法在类路径中找到该类.

I have spring boot application packaged as executable jar. Application has one utility class with main method which I want to invoke through command line but it seems that command is unable to find the class in classpath.

命令:

java -cp client-stp-grid-publisher-SNAPSHOT.jar com.client.stp.util.JMXClientUtils

我尝试使用gradle jar任务在MANIFEST.MF中设置jar类路径,但没有帮助:

I tried to set jar classpath in MANIFEST.MF using gradle jar task but no help:

jar {
    baseName = 'client-stp-grid-publisher'
    manifest {
        attributes(
                'Class-Path': "BOOT-INF/classes/*"
        )
    }
}

生成的MANIFEST.MF:

Generated MANIFEST.MF:

Manifest-Version: 1.0
Main-Class: org.springframework.boot.loader.JarLauncher
Class-path: BOOT-INF/classes/*
Start-Class: com.client.stp.ClientStpGridPublisherMain
Spring-Boot-Version: 1.5.10.RELEASE
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/

我了解理想情况下不能将可执行jar用作依赖库,但是有没有解决方法来实现我的用例?

I understand that ideally executable jar can't be used as dependent library but is there any workaround to achieve my use case?

其他解决方案可以通过传递一个标志来将主类或实用程序类作为Spring Boot应用程序运行,但我不想将实用程序类作为Spring Boot进程运行.

Other solution can be pass a flag to run main class or utility class as spring boot application but I don't want to run utility class as spring boot process.

推荐答案

也许可以用命令进行测试

may be you can test with command

java -cp client-stp-grid-publisher-SNAPSHOT.jar -Dloader.main=com.client.stp.util.JMXClientUtils org.springframework.boot.loader.PropertiesLauncher

可以在此处中找到.

这篇关于通过命令行运行Spring Boot应用程序的非主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:40