javax script how to call a function in JavaScript from Java

Posted by Dan Howard on Stack Overflow See other posts from Stack Overflow or by Dan Howard
Published on 2010-04-25T23:09:33Z Indexed on 2010/04/25 23:13 UTC
Read the original article Hit count: 337

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about javax.script

Related posts about java