I recently found a strange problem, I used code similar to the following in the project: public static void main(String[] args) throws ScriptException, NoSuchMethodException { ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); ScriptEngine engine = scriptEngineManager.getEngineByName("nashorn"); Compilable ce = (Compilable) engine; CompiledScript cs; try { String script = "function f1602838289254hw9b() {\n" + "var responses = JSON.parse(org.example.MockHttpUtils.sendGetRequest([{\n" + "url:'https://rz.xx.com/test',\n" + "params: {\n" + "id: 0,\n" + "// uuid\n" + "uuid:1,\n" + "styleid: \")\",\n" + "// styleId\n" + "templateid: 0 // backend templateId\n" + "}\n" + "}]));\n" + "var e = responses[2]\n" + "return responses\n" + "}"; ce.compile(script); engine.eval(script); } catch (Exception e) { e.printStackTrace(); } System.out.println(JSON.toJSONString(((Invocable) engine).invokeFunction("f1602838289254hw9b"))); }
I first perform a pre-compilation, and then I will execute the js script logic by calling function, but it can compile normally, but when the invokeFunction is executed, the method name is passed in, and an error is reported: java.lang.NoSuchMethodException: No such function I re-compile it again, and invokeFunction can be executed normally. I don't know what caused this, and this problem seems to be unable to reproduce stably. Another curious point is why the invokeFunction method needs to be executed first after the evel method is executed. What do compile and eval do below? ScriptEngine engine = scriptEngineManager.getEngineByName("nashorn"); Compilable ce = (Compilable) engine; ce.compile(script); engine.eval(script); I understand that compile has done everything and can directly execute invokeFunction. -- Sent from: http://nashorn.36665.n7.nabble.com/