How to use Java Processbuilder to execute a class from another class

Tags:



I am a student and pretty a novice in coding. I am trying to write a UDP-Server-Client project and execute Server, Client as Processes . However i don’t understand how to use the Processbuilder to do that. I am pretty much went through tons of related topics but i still can’t understand it. Which parameters should i pass in in this particular Program ? Code below:

Main.java

package praktikum;
import java.io.IOException;

public class main {

    public static void main(String[] args) throws IOException {

        ProcessBuilder pb1 = new ProcessBuilder("java", "-cp", ".","praktikum.Server");
        ProcessBuilder pb2 = new ProcessBuilder("java", "-cp", ".","praktikum.Client");
        Process p1 = pb1.start();
        Process p2 = pb2.start();

    }
}

Client.java

package praktikum;
import java.io.IOException;
import java.net.*;
import java.util.Random;

public class Client {

    public static void main(String[] args) throws IOException {
        String test = "This Work !";
        DatagramSocket ds = new DatagramSocket();
        int port = 1234;
        InetAddress ia = InetAddress.getLocalHost();
        byte[] data = new byte[1024];
        data = test.getBytes();
        DatagramPacket dp= new DatagramPacket(data,data.length,ia, port);
        ds.send(dp);
    }
}

Server.java

package praktikum;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Server {
    public static void main(String[] args) throws IOException {
        byte[] buf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buf, buf.length);
        DatagramSocket ds = new DatagramSocket(1234);
        ds.receive(dp);
        String str =new String(dp.getData(),0,dp.getLength());
        String ipAddress = String.valueOf(dp.getAddress());
        int port = dp.getPort();

        System.out.println("Server-> IP : " + ipAddress + " | Port : " + port + " | Information : " + str + "n");


    }
}

And there is no error. The console print out nothing. Thanks !!

Answer

You don’t see any output because you are not reading the standard output of your processes from your Main class.

There are several ways to do it but let’s stick to ProcessBuilder‘s inheritIO() method for the sake of simplicity.

package praktikum;

import java.io.IOException;

public class Main
{

    public static void main(String[] args) throws IOException
    {
        ProcessBuilder pb1 = new ProcessBuilder("java", "-cp", ".", "praktikum.Server");
        // This will make sure the standard input and output of your subprocess pb1 
        // are the same as this process (Main.java)
        pb1.inheritIO();

        ProcessBuilder pb2 = new ProcessBuilder("java", "-cp", ".", "praktikum.Client");
        // This will make sure the standard input and output of your subprocess pb2
        // are the same as this process (Main.java)
        pb2.inheritIO();

        pb1.start();
        pb2.start();
    }
}

Now, when you run your Main.java, you’ll be able to see what output/errors your subprocesses are printing. If you see the errors below:

Error: Could not find or load main class praktikum.Client
Error: Could not find or load main class praktikum.Server

as a workaround, I’d advice to pass the absolute path to the ProcessBuilder instead of '.' especially if you are running from an IDE:

new ProcessBuilder("java", "-cp", "/path/to/package", "praktikum.Server");

Further reading:



Source: stackoverflow