如何在 Java 可执行文件 .jar 中启动多个主程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5957873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I start multiple main programs in a Java executable .jar?
提问by Brian
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?
我正在编写一个包含多个包的程序。每个包都有自己的主程序,当解释器执行 .jar 时,我希望所有程序都同时启动。这似乎是一个相当简单的问题,但是当我环顾四周时,似乎人们正在使用蚂蚁(我以前从未使用过)和其他方法。在 Eclipse 中是否有一种更简单的方法来编译具有多个启动配置的 .jar,更好的是,有没有办法对其进行硬编码?
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!
如果启动它的最佳方式是通过蚂蚁。如果我想启动,我会写什么样的蚂蚁脚本......说包com.myapp.package1.main,com.myapp.package2.main和com.myapp.package3.main中的主要程序。提前致谢!
回答by Synesso
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.
jar 清单允许您选择指定不超过一个主类。当您java
使用-jar
标志执行时会调用它。
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.
您可以在一个 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
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'.
上面的示例将生成三个独立的 Java VM,每个都在自己的进程中。显然,这不符合您对“可执行 jar”的要求。
Alternatively, you may wish to have one main method that starts separate threads, so that there is only one process, but concurrent execution.
或者,您可能希望有一个 main 方法来启动单独的线程,以便只有一个进程,但并发执行。
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.
Ant 不是帮助您解决此问题的合适选择。我怀疑您可能想要一个生成多个线程的 main 方法。请随时提供有关您的要求的更多信息。
回答by fmucar
You can create one main "main" class which executes the rest.
您可以创建一个主要的“主”类来执行其余的类。
回答by Serhiy
Probably I would stick to the MANIFEST solution, but there is another possibility for this to be done:
可能我会坚持使用 MANIFEST 解决方案,但还有另一种可能性:
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.
但是在这里你应该小心路径并应该正确结束进程,否则你的机器可能会达到文件描述符的限制。