Run multiple java main methods in eclipse

折月煮酒 提交于 2019-11-30 08:32:32

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.

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) {...}
}

Well, why not write a test case using JUnit. I mean,

  • 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

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.

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.

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.

Window -> New Window

Run the separate programs separately.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!