写入 Java 进程的输入流

2022-09-01 16:50:11

我有一个代码,可以启动一个java进程(即:执行编译的java代码),通过

ProcessBuilder builder = new ProcessBuilder("java", "Sample", "arg1", "arg2");
builder.redirectErrorStream(true);
Process process = builder.start();

通过这个,我基本上可以处理输出和错误

OutputStream stdin = process.getOutputStream(); // <- Eh?
InputStream stdout = process.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

// reader.readLine() blah blah

现在,如何将输入发送到 ?也就是说,如果进程执行的代码有一行等待输入,如:stdin

Scanner scan = new Scanner(System.in);
String val = scan.nextLine();
System.out.println(val);

我试过这个:

writer.write("I'm from the stdin!.");
writer.flush();

虽然什么也没发生。控制台仍在等待输入。

有什么想法吗?

编辑:这个问题已得到答复,如下文所述。我正在编辑以显示错误的代码(顺便说一句,我未能包含该代码。哈哈)。

在零件之前,我有一个writer.write()

String line;
line = reader.readLine();
while (line != null) {
    System.out.println(line);
    line = reader.readLine();
}

答案 1

(我们的观点)是从过程的角度来看的STDINProcessOutputStream

OutputStream stdin = process.getOutputStream(); // write to this

所以你拥有的应该是正确的。

我的驱动程序(使用资源试用语句应用您自己的最佳做法)

public class ProcessWriter {
    public static void main(String[] args) throws Exception {
        ProcessBuilder builder = new ProcessBuilder("java", "Test");
        builder.directory(new File("C:\\Users\\sotirios.delimanolis\\Downloads"));
        Process process = builder.start();

        OutputStream stdin = process.getOutputStream(); // <- Eh?
        InputStream stdout = process.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

        writer.write("Sup buddy");
        writer.flush();
        writer.close();

        Scanner scanner = new Scanner(stdout);
        while (scanner.hasNextLine()) {
            System.out.println(scanner.nextLine());
        }
    }
}

我的申请

public class Test {

    public static void main(String[] args) throws Exception {
        Scanner console = new Scanner(System.in);
        System.out.println("heello World");
        while(console.hasNextLine()) {
            System.out.println(console.nextLine());
        }
    }
}

运行驱动程序打印

heello World
Sup buddy

出于某种原因,我需要.单靠自己是做不到的。close()flush()

编辑如果不是您提供 .close()\n

所以与

writer.write("Sup buddy");
writer.write("\n");
writer.write("this is more\n");
writer.flush();    

驱动程序打印

heello World
Sup buddy
this is more

答案 2

这是一个也许可以帮助某人的例子

import java.io.IOException;
import java.io.File;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        String[] commands = {"C:/windows/system32/cmd.exe"};
        ProcessBuilder builder = new ProcessBuilder(commands);
        builder.directory(new File("C:/windows/system32"));
        Process process = builder.start();

        OutputStream stdin = process.getOutputStream();
        InputStream stdout = process.getInputStream();
        InputStream stderr = process.getErrorStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
        BufferedReader error = new BufferedReader(new InputStreamReader(stderr));

        new Thread(() -> {
            String read;
            try {
                while ((read = reader.readLine()) != null) {
                    System.out.println(read);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            String read;
            try {
                while ((read = error.readLine()) != null) {
                    System.out.println(read);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            while (true) {
                try {
                    Scanner scanner = new Scanner(System.in);
                    writer.write(scanner.nextLine());
                    writer.newLine();
                    writer.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

推荐