简体   繁体   中英

Can I execute Two different Classes from same jar file?

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?

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.

The short answer is, YES!

The longer answer is that you can specify the class in your command line as well as putting the .jar in the classpath.

Assume two classes

A.class 
B.class

jar them into a .jar file

jar cvf AB.jar A.class B.class

run either of them

java -classpath AB.jar A
java -classpath AB.jar B

假设您的Server和Client类都有main方法,您可以使用以下命令从命令行执行它们:

java -cp jarFile.jar package1.Server

java -cp jarFile.jar package2.Client

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM