写入 Java 进程的 InputStream



我有一个代码,它通过以下方式启动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();
}

从过程的角度来看,Process OutputStream(我们的观点)是STDIN。

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 moren");
writer.flush();    

驱动程序打印

heello World
Sup buddy
this is more

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

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

相关内容

  • 没有找到相关文章

最新更新