我试图通过Jython在Python中使用原始eval。 但由于某些原因我不明白,我得到一个NullPointerExec。
public static String Parse(String s)
{
ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
try
{
return engine.eval("eval('%s')".format(s)).toString();
}
catch (ScriptException e)
{
e.printStackTrace();
}
return "--";
}
答案 0 :(得分:0)
您可以通过Java(和Jython)使用Pythons eval函数,如下所示:
public static boolean XLargerThanY(int x, int y) throws ScriptException {
PyScriptEngineFactory factory = new PyScriptEngineFactory();
ScriptEngine engine = factory.getScriptEngine();
PyFunction function = (PyFunction) engine.eval("x > y");
PyBoolean ans = (PyBoolean) function.__call__(new PyInteger(x), new PyInteger(y));
return ans.getBooleanValue();
}