当我输入 Python 脚本时,例如2 + 2
,它返回4
. 这就是我想要的。但是当我尝试输入时print 'hello world!'
,出现异常!我只想从执行的 Python 脚本中获取解释器的输出。
private PythonInterpreter coder = new PythonInterpreter();
private String script;
private PyObject interp;
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
try
{
coder.exec(script); // this will execute the python script
interp = coder.eval(script); // this is what i use in trying to catch the interpreter's output
System.out.println(interp); // and trying to see what it returns
}
catch(Exception s)
{
JOptionPane.showMessageDialog(this, s, script, WIDTH, null);
}
}