如何在 Java 可执行文件 .jar 中启动多个主程序?

发布于 2024-11-06 00:15:41 字数 300 浏览 4 评论 0原文

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

如果启动这个的最好方法是通过蚂蚁。如果我想启动,我会写什么样的ant脚本...比如包com.myapp.package1.main、com.myapp.package2.main和com.myapp.package3.main中的主程序。提前致谢!

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?

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

金橙橙 2024-11-13 00:15:41

jar 清单允许您有选择地指定不超过一个主类。当您使用 -jar 标志执行 java 时,会调用此函数。

java -jar myapp.jar

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

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”的要求。

或者,您可能希望有一个启动单独线程的主方法,以便只有一个进程,但并发执行。

Ant 不是帮助您解决此问题的合适选择。我怀疑您可能想要一个生成多个线程的主方法。请随时提供有关您的要求的更多信息。

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.

将军与妓 2024-11-13 00:15:41

您可以创建一个主要的“主”类来执行其余部分。

You can create one main "main" class which executes the rest.

孤凫 2024-11-13 00:15:41

也许我会坚持使用 MANIFEST 解决方案,但还有另一种可能性:

Process p = Runtime.getRuntime().exec("java -jar another.jar");

但在这里你应该小心路径并正确结束进程,否则你的机器可能会达到文件描述符的限制。

Probably I would stick to the MANIFEST solution, but there is another possibility for this to be done:

Process p = Runtime.getRuntime().exec("java -jar another.jar");

But here you should be careful with path and should properly end process, else your machine can reach the limit for file descriptors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文