Run non main class of spring boot application through command line

橙三吉。 提交于 2020-06-12 05:21:58

问题


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.

Command:

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

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/*"
        )
    }
}

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/

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

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.


回答1:


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

It can be found in here.



来源:https://stackoverflow.com/questions/50367647/run-non-main-class-of-spring-boot-application-through-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!