Skip to content Skip to sidebar Skip to footer

How Do You Invoke A Method In A Nashorn Compiledscript?

I have the following code which works: ScriptEngine jsEngine = ScriptEngineManager.new().getEngineByName('nashorn'); jsEngine.eval('some script'); jsEngine.invokeMethod(jsEngine.e

Solution 1:

You call the method?

Here are few examples: http://www.programcreek.com/java-api-examples/index.php?api=javax.script.CompiledScript


Example:

import java.util.*;
import javax.script.*;

publicclassTestBindings {
    publicstaticvoidmain(String args[])throws Exception {
        Stringscript="function doSomething() {var d = date}";
        ScriptEngineengine=newScriptEngineManager().getEngineByName("JavaScript");
        CompilablecompilingEngine= (Compilable) engine;
        CompiledScriptcscript= compilingEngine.compile(script);

        //Bindings bindings = cscript.getEngine().createBindings();Bindingsbindings= engine.getBindings(ScriptContext.ENGINE_SCOPE);
        for(Map.Entry me : bindings.entrySet()) {
            System.out.printf("%s: %s\n",me.getKey(),String.valueOf(me.getValue()));
        }
        bindings.put("date", newDate());
        //cscript.eval();
        cscript.eval(bindings);

        Invocableinvocable= (Invocable) cscript.getEngine();
        invocable.invokeFunction("doSomething");
    }
}

Post a Comment for "How Do You Invoke A Method In A Nashorn Compiledscript?"