Reputation: 151
Following on from the answer to: If I am creating a simple client server application in IntelliJ, how should this work? as I don't have enough reputation to continue the thread.
When I right-click on the class containing the main() method and select "run" the public class is added to IntelliJ's run configurations (the dropdown list to the left of the green arrow on IntelliJ's button bar) and I can choose "Edit Configurations" from this dropdown to change the command line arguments. I can run either client or server by putting "client" or "server" as the argument. How do I run the server and then the client classes from the same project? Do I need two main() methods, one in each class?
import java.io.IOException;
public class serversocket {
public static void main(String args[]) throws IOException {
System.out.println(args[0]); //debug
if (args[0] == "client") {
runClient();
} else if (args[0] == "server") {
runServer();
} else {
System.out.println("no arguments.");
}
}
private static void runClient() throws IOException {
System.out.println("Client running..."); //test
}
private static void runServer() throws IOException {
System.out.println("Server running..."); //test
}
Upvotes: 0
Views: 3043
Reputation: 972
Just copy configuration, name first conf as server
and second as client
and provide correct argument, then run both. It should run two seperate java processes. To copy configuration use third button from right at the top-left panel in configurations dialog.
But I think seperating server code from client code to at least two different classes with two main methods will be a good choice.
Upvotes: 1