2017-09-19 43 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月發行版解決了此問題。