java程序可以有多个main方法,在eclipse中运行多个java main方法

I'm running Eclipse 3.5 and I have a frequent problem that in order to test my program, I have to do about 6-7 clicks as opposed to one click on the play button.

The problem is I'm writing networking application and thus I have a run config for "Server" and a run config for "Client". Then to test my program I have to start the server, then a client, then another client etc. Is there anyway to automate this into one run configuration?

解决方案

You can call the main method of any class directly. For example, if you have Server and Client class and you want to run one server and two client, here is what you may do.

public class Server {

public void main(final String ... $Args) {

final Server S = new Server();

S.config($Args);

S.run();

}

}

public class Client {

public void main(final String ... $Args) {

final Client C = new Client();

C.config($Args);

C.run();

}

}

public class Test_ServerClient {

public void main(final String ... $Args) {

Server.main('server1.cfg');

Client.main('client1.cfg');

Client.main('client2.cfg');

}

}

Done!

Well, almost. You may want do some delay before calling main of the client just to make sure server is up and running properly.

One think though. All the Server and Clients will be run on the same JVM. In most case (that you just want to test its interaction and have nothing to do with class loading as that will behave differently when they are/are not on the same JVM), this should be fine. If you really want o make it run on different JVM, you may use Ant to run them instead.

Something like this:

So you can just run this ant and that is it.

Hope this helps.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值