如何在Java可执行文件.jar中启动多个主程序? [英] How do I start multiple main programs in a Java executable .jar?

查看:520
本文介绍了如何在Java可执行文件.jar中启动多个主程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个包含多个包的程序。每个包都有自己的主程序,我希望在解释器执行.jar时同时启动它们。这似乎是一个相当简单的问题,但当我环顾四周时,似乎人们正在使用蚂蚁(我之前从未使用过)和其他方法。在Eclipse中是否有一种更简单的方法来编译具有多个启动配置的.jar,更好的是,有没有办法对其进行硬编码?

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 interpreter. This seems like a fairly simple question, but when I looked around, it seems that people are using ants (which I've never used before) and other methods. Is there a simpler way in Eclipse to compile a .jar with multiple launch configurations, better yet, is there a way to hard code it in?

如果最好的方法是这是通过蚂蚁发起的。如果我想要发布,我会写什么样的蚂蚁脚本...说com.myapp.package1.main,com.myapp.package2.main和com.myapp.package3.main包中的主要程序。提前致谢!

If the best way to launch this is through an ant. What kind of ant script would I write if I want to the launch... say the main programs in packets com.myapp.package1.main, com.myapp.package2.main, and com.myapp.package3.main. Thanks in advance!

推荐答案

jar清单允许您选择指定不超过一个主类。使用 -jar 标志执行 java 时会调用此方法。

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

你可以在一个jar中包含多个主类,但必须使用 -classpath 标志并使用指定的主类的完全限定名称来调用每个主类(上面的可选1除外) 。

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

示例上面将生成三个独立的java VM,每个VM都在自己的进程中。显然,这不符合您对'可执行jar'的要求。

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不是帮助您解决此问题的合适选择。我怀疑你可能想要一个产生多个线程的主要方法。随时提供有关您的要求的更多信息。

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.

这篇关于如何在Java可执行文件.jar中启动多个主程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆