In one package I have two different classes Client.java and Server.java I want to make this package jar, i mean executable. First I want the Server class to run and after 2-3 seconds I want Client method to run. Is it possible?
Thank you
You have to leave only one main method and run your server and client in separate threads from it.
To do it, take a look at Runnable interface. Your server class and client class should implement it. Then you have to move the logic, used to start server and client to it's run()
method.
class Server implements Runnable {
@Override
public void run() {
//your server starting logic here
}
}
class Client implements Runnable {
@Override
public void run() {
//your client starting logic here
}
}
After that, you can modify your main
method, to start server and client, like:
public static void main(String args[]) throws InterruptedException {
Server server = new Server();
Client client = new Client();
Thread tServer = new Thread(server);
tServer.start();
//here you can wait some time to Server started
Thread tClient = new Thread(client);
tClient.start();
}