25

我有一个启动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();
}
4

2 回答 2

51

Process OutputStream我们的观点)是从进程的角度来看的 STDIN

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

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

我的驱动程序(通过 try-with-resources 语句应用您自己的最佳实践)

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
于 2013-09-19T19:44:18.133 回答
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();
    }
}
于 2021-11-29T14:07:29.767 回答