我有一个运行python脚本的java程序(该脚本不是我的,因此无法更改)。 我正在运行脚本:
Process p = Runtime.getRuntime().exec("python myScript.py");
该脚本有一个“raw_input”行,需要用户输入。 我尝试使用
BufferedWriter userInput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
userInput.write("myInput");
但这似乎不起作用;
另一件事,我正在用
读取输出BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
List<String> output = new ArrayList();
while ((s = stdInput.readLine()) != null) {
output.add(s);
}
当python脚本不期望任何输入时,这是有效的,但是当有一个input_raw()时,stdInput.readLine()就会卡住。
python脚本的一个例子:
name = raw_input("What is your name? ")
print "your name is "+name
整个计划:
public static void main(String args[]) {
PythonRunner pr = new PythonRunner();
pr.start();
while(!pr.isFinished){
try {
System.out.println("waiting...");
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for(String s:pr.result) System.out.println(s);
}
public class PythonRunner extends Thread {
public List<String> result;
public boolean isFinished;
public PythonRunner() {
result= new ArrayList<>();
isFinished = false;
}
@Override
public void run(){
try {
Process p = Runtime.getRuntime().exec("python myScript.py");
BufferedWriter userInput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
userInput.write("myInput");
String s;
while ((s = stdInput.readLine()) != null) {
result.add(s);
}
isFinished=true;
}
catch (IOException e) {
isFinished=true;
}
}
}
修改 我设法使用
为脚本提供输入userInput.write(cmd);
userInput.newLine();
userInput.flush();
但是,我仍然无法阅读输出。一些脚本具有无限循环的输入 - &gt;打印。 例如,脚本:
stop = False
while not stop:
name = raw_input("")
print "your name is "+name
if name == "stop":
stop = True
当这个脚本运行时,我似乎无法读取已经给定名称的输出。只有当进程被给出“stop”命令时,才能读取整个输出。
答案 0 :(得分:0)
您需要继承IO
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#inheritIO()
将子进程标准I / O的源和目标设置为 与当前Java进程的那些相同。这是一种方便 方法。调用表单
pb.inheritIO()
然后,您的流程执行可以采取以下形式
public class ProcessSample {
public static void main(String [] arg) throws Exception {
ProcessBuilder pb =
new ProcessBuilder("python", "script.py").inheritIO();
Process p = pb.start();
p.waitFor();
}
}
,并使用您的脚本
name = raw_input("What is your name? ")
print "your name is "+name
你可以做到
> javac ProcessSample.java
> java -cp . ProcessSample
What is your name? a
your name is a
为stdout继承IO
您可以随时将一些值传递给Python代码,同时通过继承它来读取stdout中的值。
import java.io.*;
public class PythonProcessRedirect {
public static void main(String [] arg) throws Exception {
ProcessBuilder pb =
new ProcessBuilder("python", "script_raw.py");
pb.redirectErrorStream(true);
pb.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process p = pb.start();
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( p.getOutputStream() ));
String input = "2\n";
writer.write(input);
writer.flush();
p.waitFor();
}
}
请注意,对于连续的数据流,您需要将其刷新
import sys
import time
sys.stdout.write("give some input: ")
sys.stdout.flush()
time.sleep(2)
line = sys.stdin.readline()
sys.stdout.write("you typed: " + line)
sys.stdout.flush();
否则,数据将在Python刷新之前提供(例如,通过填充缓冲区达到限制)。
Stream Gobbler
你也可以使用Stream Gobbler并为stdin / stdout / stderr运行单独的线程。
import java.io.*;
public class PythonProcessStreamGobblerIO {
public static void main(String [] arg) throws Exception {
final Process p = Runtime.getRuntime().exec("python ./script_os.py" );
new Thread() {
public void run() {
try {
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter( p.getOutputStream() ));
String input = "2\n";
writer.write(input);
writer.flush();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}.start();
new Thread() {
public void run() {
try {
Reader reader = new InputStreamReader(p.getInputStream());
int data = -1;
while((data =reader.read())!= -1){
char c = (char) data;
System.out.print(c);
}
reader.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}.start();
p.waitFor();
}
}
但是,再一次,你必须确保(在Python端)刷新stdout。