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

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

    You can execute any class which has a public final static main method from a JAR file, even if the jar file has a Main-Class defined.

    Execute Main-Class:

    java -jar MyJar.jar  // will execute the Main-Class
    

    Execute another class with a public static void main method:

    java -cp MyJar.jar com.mycomp.myproj.AnotherClassWithMainMethod
    

    Note: the first uses -jar, the second uses -cp.

提交回复
热议问题