How do I start multiple main programs in a Java executable .jar?

前端 未结 3 755
暖寄归人
暖寄归人 2020-12-25 15:28

I\'m writing a program that contains multiple packages in it. Each package has its own main program that I want all to launch simultaneously when the .jar is executed by an

3条回答
  •  执笔经年
    2020-12-25 15:50

    The jar manifest allows you to optionally specify no more than one main class. This is invoked when you execute java with the -jar flag.

    java -jar myapp.jar
    

    You may include multiple main classes in a single jar, but each (except the optional 1 above) must be invoked using the -classpath flag and with the fully qualified name of the main class specified.

    java -classpath myapp.jar com.mypackage.app.Main01 && \
      java -classpath myapp.jar com.mypackage.app.Main02 && \
      java -classpath myapp.jar com.mypackage.app.Main03
    

    The example above will spawn three separate java VMs, each in their own process. Obviously, this does not meet your requirement for an 'executable jar'.

    Alternatively, you may wish to have one main method that starts separate threads, so that there is only one process, but concurrent execution.

    Ant is not a suitable choice to help you solve this issue. I suspect you probably want a single main method that spawns multiple threads. Feel free to provide more information on your requirements.

提交回复
热议问题