简体   繁体   中英

Run multiple java main methods in eclipse

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.

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.

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