问题描述
我有一个启动java进程的代码(即:执行已编译的java代码)
I have a code that starts a java process (i.e.: executing a compiled java code) via
ProcessBuilder builder = new ProcessBuilder("java", "Sample", "arg1", "arg2");
builder.redirectErrorStream(true);
Process process = builder.start();
通过这个,我基本上可以处理输出和错误
Through this, I can basically process the output and errors
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
?也就是说,如果进程执行的代码有一行等待输入,如下所示:
Now, how can I send input to the stdin
? That is, if the code executed by the process has a line that waits for an input as in:
Scanner scan = new Scanner(System.in);
String val = scan.nextLine();
System.out.println(val);
我试过这个:
writer.write("I'm from the stdin!.");
writer.flush();
虽然什么也没发生。控制台仍在等待输入。
Though nothing happened. The console still waited for an input.
有什么想法吗?
编辑:如下所述,问题得到了回答。我正在编辑以显示错误代码(我未能包括顺便说一句.Lol)。
The question was answered, as accepted below. I'm editing to show the faulty code (which I failed to include btw. Lol).
在之前writer.write()
部分,我有一个
String line;
line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
推荐答案
处理
OuputStream
(我们的观点)是从流程的角度来看STDIN
The Process
OuputStream
(our point of view) is the STDIN from the process point of view
OutputStream stdin = process.getOutputStream(); // write to this
所以你所拥有的应该是正确的。
So what you have should be correct.
我的驱动程序(使用try-with-resources语句应用您自己的最佳实践)
My driver (apply your own best practices with try-with-resources statements)
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()
将不会这样做。
For some reason I need the close()
. The flush()
alone won't do it.
编辑它也有效如果不是 close()
,你提供 \ n
。
Edit It also works if instead of the close()
you provide a \n
.
所以
writer.write("Sup buddy");
writer.write("\n");
writer.write("this is more\n");
writer.flush();
驱动程序打印
heello World
Sup buddy
this is more
这篇关于写入Java进程的InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!