简体   繁体   English

如何使用 Java Processbuilder 从另一个 class 执行 class

[英]How to use Java Processbuilder to execute a class from another class

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.我正在尝试编写一个 UDP-Server-Client 项目并将服务器、客户端作为进程执行。 However i don't understand how to use the Processbuilder to do that.但是我不明白如何使用 Processbuilder 来做到这一点。 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主.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客户端.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服务器.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 !!谢谢 !!

You don't see any output because you are not reading the standard output of your processes from your Main class.您看不到任何 output,因为您没有从主 class 读取您的流程的标准 output。

There are several ways to do it but let's stick to ProcessBuilder 's inheritIO() method for the sake of simplicity.有几种方法可以做到这一点,但为了简单起见,让我们坚持使用ProcessBuilderinheritIO()方法。

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.现在,当您运行 Main.java 时,您将能够看到您的子进程正在打印哪些输出/错误。 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 '.'作为一种解决方法,我建议将绝对路径传递给 ProcessBuilder 而不是'.' especially if you are running from an IDE:特别是如果您从 IDE 运行:

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

Further reading:进一步阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM