JavaScript engine for Java

狂风中的少年 提交于 2019-12-06 09:20:35
  1. Are you absolutely sure you really need eval? There are very, very few places where eval is actually necessary.

  2. You can use ProcessBuilder to shell out to any process available to the underlying system. I'd say the odds of it being faster than a Rhino eval are low.

  3. You might keep a NodeJS process running alongside your app which you communicate with via a socket. That might win a speed race with eval in Rhino.

If you give an example of what you're actually trying to achieve, it may be that people can come up with a better approach for you.

Have a look at javax.script.ScriptEngine. It's a standard Java package, allows evals and data binding:

ScriptEngineManager engineMgr = new ScriptEngineManager();
ScriptEngine engine = engineMgr.getEngineByName("JavaScript");
Bindings bindings = engine.createBindings();

String script = "javascript to eval goes here.....";
bindings.put(varName1, value1);
bindings.put(varName2, value2);

Object obj = engine.eval(script, bindings)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!