Java 如何使用apache common exec运行交互式python脚本?
我正在尝试使用ApacheCommonsExec运行python脚本。我需要将一些值作为交互脚本中的python脚本传递给python脚本。如何操作 我的尝试是在父进程的输入流中设置值,但它对我不起作用 到目前为止我的代码:Java 如何使用apache common exec运行交互式python脚本?,java,stream,external-process,apache-commons-exec,Java,Stream,External Process,Apache Commons Exec,我正在尝试使用ApacheCommonsExec运行python脚本。我需要将一些值作为交互脚本中的python脚本传递给python脚本。如何操作 我的尝试是在父进程的输入流中设置值,但它对我不起作用 到目前为止我的代码: String line = "python /home/abhijeet/test.py"; CommandLine cmdLine = CommandLine.parse(line); byte buf[]="4".getBytes(); I
String line = "python /home/abhijeet/test.py";
CommandLine cmdLine = CommandLine.parse(line);
byte buf[]="4".getBytes();
InputStream io=new ByteArrayInputStream(buf);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
PumpStreamHandler streamhandler=new PumpStreamHandler(System.out,System.err,io);
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(streamhandler);
executor.execute(cmdLine, resultHandler);
try
{
resultHandler.waitFor();
}
catch (InterruptedException e)
{
System.out.println("yo errior");
e.printStackTrace();
}
根据Apache文档,我猜您需要使用setProcessInputStream(OutputStream os):“设置OutputStream,通过它可以将输入发送到进程。”,将您的字节写入输出流并调用此函数。@Batshray:在您回答该问题之前,我尝试过这样做,但即使这样也不起作用。它起作用了,但它会等待我在控制台中输入值,如果我将该值输入控制台,它也会正常工作。我不想这样做,谢谢您的帮助:)不客气-阿比吉特Panwar@BatScream你能检查一下吗?我可以发送第一个值,但不知道如何发送其余的值,因为我无法使用io.write()写入多个值。@BatScream:我尝试了OutputStream os=new ByteArrayOutputStream();写入(“5.getBytes());os.flush();setProcessInputStream(os);创建流处理程序后的代码。但它不起作用:(