I am trying to use the original eval in Python, via Jython. But for some reason I don't understand, I am getting a 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 "--";
}
You can use Pythons eval function through Java (and Jython) like this:
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.