How to run a class from Jar which is not the Main-Class in its Manifest file

后端 未结 7 2190
情深已故
情深已故 2020-11-28 01:39

I have a JAR with 4 classes, each one has Main method. I want to be able to run each one of those as per the need. I am trying to run it from command-line on Linux box.

相关标签:
7条回答
  • 2020-11-28 02:35

    This answer is for Spring-boot users:

    If your JAR was from a Spring-boot project and created using the command mvn package spring-boot:repackage, the above "-cp" method won't work. You will get:

    Error: Could not find or load main class your.alternative.class.path

    even if you can see the class in the JAR by jar tvf yours.jar.

    In this case, run your alternative class by the following command:

    java -cp yours.jar -Dloader.main=your.alternative.class.path org.springframework.boot.loader.PropertiesLauncher
    

    As I understood, the Spring-boot's org.springframework.boot.loader.PropertiesLauncher class serves as a dispatching entrance class, and the -Dloader.main parameter tells it what to run.

    Reference: https://github.com/spring-projects/spring-boot/issues/20404

    0 讨论(0)
提交回复
热议问题