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?
From the above program, we can say that Java can have multiple main methods but with the concept of overloading. There should be only one main method with parameter as string[ ] arg.
Hence there is not conflict amongst the multiple classes having main method. We can overload main method but we can not override it. So , we can have many main methods in a class.
Yes you can have more classes that contain public static void main(String[] args) . And you can chose to execute one class or another class. However, you can't have more than one main method within same class.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With