Java Nashorn expose java class (not instance) to javascript -
i using clearscript
.net
expose classes javascript. used expose class (not instance of class js) : engine.addhosttype("worker", typeof(worker));
use var x = new worker();
in javascript
;
now in nashorn
java not work. able expose instance of class : factory.getbindings().put("worker", new worker());
is there way expose class type javascript nashorn.
thank you!
your script can directly access java class qualified name such as
var vector = java.type("java.util.vector"); // or equivalently: var vector = java.util.vector; // java.type better throws exception if class not found
if don't want script directly refer java class or perhaps want provide different name, this:
import javax.script.*; public class main { public static void main(string[] args) throws scriptexception { scriptenginemanager m = new scriptenginemanager(); scriptengine e = m.getenginebyname("nashorn"); // eval "java.util.vector" "type" object , // assign global variable. e.put("vec", e.eval("java.util.vector")); // script can use "vec" though script constructor system.out.println(e.eval("v = new vec(); v.add('hello'); v")); } }
hope helps!
Comments
Post a Comment