我可以从同一个jar文件中执行两个不同的类吗? [英] Can I execute Two different Classes from same jar file?
问题描述
我有一个项目,我在一个包中创建了Server,在第二个包中我创建了Client。它工作正常。我想创建一个Jar文件。是否可以从同一个jar文件中单独运行客户端和服务器?
I have a project where in one package I have made Server and in Second Package I made Client. It is working fine. I want to create a Jar file. Is it possible to run Client and Server Separately from same jar file?
我已经使用了jar文件,其中只有一个main,当我运行jar文件时,它会自动运行该类。现在,我将有2个可执行类。我想将它们打包在一个jar中然后想要从同一个jar文件中运行它们。可能吗?任何建议,链接都会有很大的帮助。
I have used jar file where there is only one main and when I run jar file, it automatically runs that class. Now here I will be having 2 executable classes. I want to pack them in only one jar and then want to run both from that same jar file. Is it possible? Any Suggestions, links will be of great help.
推荐答案
简短的回答是,是的!
答案越长,您可以在命令行中指定类,并将.jar放在类路径中。
The longer answer is that you can specify the class in your command line as well as putting the .jar in the classpath.
假设有两个类
A.class
B.class
将它们装入.jar文件
jar them into a .jar file
jar cvf AB.jar A.class B.class
运行其中任何一个
java -classpath AB.jar A
java -classpath AB.jar B
这篇关于我可以从同一个jar文件中执行两个不同的类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!