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

后端 未结 7 2227
情深已故
情深已故 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:23

    Another similar option that I think Nick briefly alluded to in the comments is to create multiple wrapper jars. I haven't tried it, but I think they could be completely empty other than the manifest file, which should specify the main class to load as well as the inclusion of the MyJar.jar to the classpath.

    MyJar1.jar\META-INF\MANIFEST.MF

    Manifest-Version: 1.0
    Main-Class: com.mycomp.myproj.dir1.MainClass1
    Class-Path: MyJar.jar
    

    MyJar2.jar\META-INF\MANIFEST.MF

    Manifest-Version: 1.0
    Main-Class: com.mycomp.myproj.dir2.MainClass2
    Class-Path: MyJar.jar
    

    etc. Then just run it with java -jar MyJar2.jar

提交回复
热议问题