Search code examples
javajavascriptjava-8nashorn

Nashorn. Binding native Java objects?


I'd like to put native Java objects into the ScriptEngine bindings for easier access.
I mean to avoid lots of Java.type(...).
I tried in that way.

jsEngine.getContext().getBindings(ScriptContext.ENGINE_SCOPE).put("manager", Manager.getInstance());

But that's failed with error "Manager has no such function "funcName" in eval...".

Is it possible at all?

UPD:
Example code

public class ManagerClass {

    public void test()
    {
        System.out.println("Hello");
    }

    public static void test2()
    {
        System.out.println("Hello Static");
    }
}

public class NewClass {
    public static void main(String[] args) throws ScriptException {
        final ScriptEngine s = new ScriptEngineManager().getEngineByExtension("js");
        s.getBindings(ScriptContext.ENGINE_SCOPE).put("manager", new ManagerClass());
        s.eval("manager.test(); manager.test2();");
    }
}

Solution

  • Solved. The correct way is

     s.eval("manager.test(); manager.class.static.test2();");