问题描述
我正在运行Eclipse 3.5,而且我经常遇到一个问题,为了测试我的程序,我必须做大约6-7个点击,而不是点击播放按钮。问题是我正在编写网络应用程序,因此我有一个服务器的运行配置和客户端的运行配置。然后测试我的程序,我必须启动服务器,然后一个客户端,然后另一个客户端等。有没有办法自动化到一个运行配置?
可以直接调用任何类的main方法。例如,如果您有Server和Client类,并且要运行一个服务器和两个客户端,那么您可以执行此操作。
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');
}
}
完成!
几乎。您可能需要在调用客户端之前做一些延迟,以确保服务器正常运行。
一个想法。所有服务器和客户端都将在同一个JVM上运行。在大多数情况下(你只是想测试它的交互,并且与类加载无关,因为它们不在同一个JVM上的行为会不同),这应该是正常的。如果你真的想让它运行在不同的JVM上,你可以使用Ant来运行它们。
这样的一个例子:
< project name =TestServerClientdefault =testbasedir =。>
< target name =test>
< java classname =my.Server>
< arg value =server1.cfg/>
< classpath>
< pathelement location =dist / test.jar/>
< pathelement path =$ {java.class.path}/>
< / classpath>
< / java>
< java classname =my.Client>
< arg value =client1.cfg/>
< classpath>
< pathelement location =dist / test.jar/>
< pathelement path =$ {java.class.path}/>
< / classpath>
< / java>
< java classname =my.Client>
< arg value =client2.cfg/>
< classpath>
< pathelement location =dist / test.jar/>
< pathelement path =$ {java.class.path}/>
< / classpath>
< / java>
< / target>
< / project>
所以你可以运行这个蚂蚁,就是这样。
希望这有帮助。
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:
<project name="TestServerClient" default="test" basedir=".">
<target name="test">
<java classname="my.Server">
<arg value="server1.cfg"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
<java classname="my.Client">
<arg value="client1.cfg"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
<java classname="my.Client">
<arg value="client2.cfg"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
</target>
</project>
So you can just run this ant and that is it.
Hope this helps.
这篇关于在eclipse中运行多个java main方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!