2017-09-19 42 views
0

我正在使用org.python.util.PythonInterpreter类在java中执行python代码。请在下面的代码片段中找到。PythonInterpreter exec函数返回空输出流

PythonInterpreter pythonInterpreter = new PythonInterpreter(null, new PySystemState()); 

ByteArrayOutputStream outStream = new ByteArrayOutputStream(16384); 

pythonInterpreter.setOut(outStream); 
pythonInterpreter.setErr(outStream); 

// execute the code 
pythonInterpreter.exec(script); 

String consoleOutput = outStream.toString(); 
outStream.flush(); 

System.out.println("Console output :- "+consoleOutput); 

与上面的代码的问题是对于相同脚本有时我得到“consoleOutput”空。我无法弄清楚问题所在。为了运行上面的代码1000次,至少4次我得到空输出。

,如果我使用默认的构造函数如下图所示它另一方面的工作就好了

PythonInterpreter pythonInterpreter = new PythonInterpreter(); 

回答

0

挖掘更多的进入我发现这个问题,设置属性python.site.importfalse触发此问题。此问题出现在Jython独立版本2.7.0中。更新到独立jar(2.7.1)的2017年6月发行版解决了此问题。