在eclipse中运行多个java main方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2189684/
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
Run multiple java main methods in eclipse
提问by aramadia
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.
我正在运行 Eclipse 3.5,我经常遇到一个问题,为了测试我的程序,我必须点击大约 6-7 次,而不是在播放按钮上点击一次。
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?
问题是我正在编写网络应用程序,因此我有一个“服务器”的运行配置和一个“客户端”的运行配置。然后为了测试我的程序,我必须启动服务器,然后是一个客户端,然后是另一个客户端等等。反正有没有将它自动化到一个运行配置中?
采纳答案by NawaMan
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.
您可以直接调用任何类的 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');
}
}
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.
嗯,差不多。您可能需要在调用客户端的 main 之前做一些延迟,以确保服务器启动并正常运行。
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.
不过一想。所有的服务器和客户端都将运行在同一个 JVM 上。在大多数情况下(您只是想测试它的交互,而与类加载无关,因为当它们位于/不在同一个 JVM 上时,它们的行为会有所不同),这应该没问题。如果你真的想让它在不同的 JVM 上运行,你可以使用 Ant 来运行它们。
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.
希望这可以帮助。
回答by Adeel Ansari
Well, why not write a test case using JUnit. I mean,
那么,为什么不使用 JUnit 编写一个测试用例。我的意思是,
- Prepare 2 properties file, one for the server and another for the client
- Write a test fixture, which starts a server and multiple clients in order to run the test
- Then write your test cases based on that
- 准备 2 个属性文件,一个用于服务器,另一个用于客户端
- 编写一个测试装置,它启动一个服务器和多个客户端以运行测试
- 然后在此基础上编写您的测试用例
I know that we should not call it a unit test, as it would be depending on network IO. So, call it whatever feels appropriate. My suggestion is just to use JUnit for this.
我知道我们不应该称它为单元测试,因为它取决于网络 IO。因此,请随意称呼它为合适的名称。我的建议只是为此使用 JUnit。
回答by Suraj Chandran
You can write a simple starter program that will start all the other programs in a new JVM. To invoke in a new JVM you can use Runtime.exec() methods.
您可以编写一个简单的启动程序来启动新 JVM 中的所有其他程序。要在新的 JVM 中调用,您可以使用 Runtime.exec() 方法。
That's how its done in lot of unit testing senarios.
这就是它在许多单元测试场景中的做法。
Your starter program could take a list of other programs as arguments and fork them each in a new java process.
您的启动程序可以将其他程序的列表作为参数,并在一个新的 Java 进程中将它们分叉。
回答by Julien Ribon
Just to complete the validated answer: If you want to execute server and clients in parallel, you need to start a new thread for each client and server instance. You can do this as follow:
只是为了完成经过验证的答案:如果要并行执行服务器和客户端,则需要为每个客户端和服务器实例启动一个新线程。您可以按如下方式执行此操作:
pulic class Test_ServerClient {
public static void main(final String[] args) {
Thread serverThread = new Thread() {
public void run() {
Server.main(args);
}
};
Thread clientThread = new Thread() {
public void run() {
Client.main(args);
}
};
serverThread.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
clientThread.start();
}
}
public class Server {
public static void main(final String[] args) {...}
}
public class Client {
public static void main(final String[] args) {...}
}
回答by Shevon Silva
Window -> New Window
窗口 -> 新窗口
Run the separate programs separately.
分别运行单独的程序。