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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -