javax script how to call a function in JavaScript from Java
- by Dan Howard
Hi All,
I'm trying to call a function in JavaScript via Java. This works fine when directly reading a script as a string but I'm using CompiledScripts.
When I do this with a compiled script it gives me method not found if I also add bindings. Without bindings it works but of course the function fails because it needs the bindings.
Any ideas?
CompiledScript script = ... get script....
Bindings bindings = script.getEngine().createBindings();
Logger scriptLogger = LogManager.getLogger("TEST_SCRIPT");
bindings.put("log", scriptLogger);
//script.eval(bindings); -- this way fails
script.eval(); // -- this way works
Invocable invocable = (Invocable) script.getEngine();
invocable.invokeFunction(methodName);
TIA