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();