How to run two Java files in VSCODE?

Viewed 1446

I'm trying to make a basic java echo client server app and the textbook I'm reading says I should run the Server.java file first and then the Client.java second. But for some reason VSCode doesn't seem to be doing that. I run my Server.java file and get this which is what I'm expecting:

Simple Echo Server
Waiting for connection.....

And then I go to my Client.java file and run that, but nothing happens there are no errors, it stays at the two lines shown above, I can CTRL+C to terminate the batch job.

I'm expecting it to say this:

Simple Echo Server
Waiting for connection.....
Connected to client

But that's not happening - I am getting no errors though. I don't think it's a problem with my code since it's identical to the textbook's but I'll post it here.

Server.java

    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server {
        public static void main(String[] args) throws Exception {
            System.out.println("Simple Echo Server");
            try (ServerSocket serverSocket = new ServerSocket(6000)) {
                System.out.println("Waiting for connection.....");
                Socket clientSocket = serverSocket.accept();
                System.out.println("Connected to client");
            } catch (IOException ex) {
    
            }
        }
    }

Client.java

import java.io.*;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws Exception {
        try {
            System.out.println("Waiting for connection.....");
            InetAddress localAddress = InetAddress.getLocalHost();
            try (Socket clientSocket = new Socket(localAddress, 6000);
                    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                    BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))) {
            }
        }

        catch (IOException ex) {

        } // Handle exceptions
    }
}

Is it possible that VS code can't run two java files at one time?

EDIT Tried the dual configuration below, but the result is the same, nothing is changing.

1 Answers

It's achievable in VS Code.

Click to create launch.json, keep the default configurations which should be similar to mine in the following picture then add compounds in it:

enter image description here

Turn to the selection box and choose compounds to run by clicking the left green triangle button, you'll get your wanted result:

enter image description here

Related