如何使用Java的ProcessBuilder在另一个类中执行一个类:

huangapple go评论82阅读模式
英文:

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

问题

以下是翻译好的部分:

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");
    }
}

以上为翻译好的代码部分。

英文:

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 !!

答案1

得分: 0

你看不到任何输出,因为你没有从你的 Main 类中读取进程的标准输出。

有几种方法可以做到这一点,但为了简单起见,我们将使用 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");
        // 这将确保子进程 pb1 的标准输入和输出与这个进程 (Main.java) 相同
        pb1.inheritIO();

        ProcessBuilder pb2 = new ProcessBuilder("java", "-cp", ".", "praktikum.Client");
        // 这将确保子进程 pb2 的标准输入和输出与这个进程 (Main.java) 相同
        pb2.inheritIO();

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

现在,当你运行 Main.java 时,你将能够看到子进程打印出的输出/错误信息。如果你看到以下错误:

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

作为解决方法,我建议传递绝对路径给 ProcessBuilder,而不是使用 '. ',特别是如果你是从 IDE 中运行的:

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

进一步阅读:

英文:

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:

huangapple
  • 本文由 发表于 2020年5月4日 22:56:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/61595186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定