Java Nashorn Run Java Function From Javascript

290 views Asked by At

I have been using ClearScript for .NET for quite a long time. I have been exposing C# functions and classes to javascript like this: engine.AddHostObject("ftp", new ftp());

After that I could do engine.eval("ftp.upload(""));

I have been trying to do something similar with nashorn for java but I can't figure it out. What would be the easiest way to do it.

2

There are 2 answers

1
David P. Caldwell On

I think this code is the Nashorn equivalent of what you've posted.

ScriptEngineManager factory = new ScriptEngineManager();
factory.getBindings().put("ftp", new ftp());
factory.getEngineByName("nashorn").eval("ftp.upload('')");
0
PiDEV On
ScriptEngineManager factory = new ScriptEngineManager();
factory.getBindings().put("ftp", new ftp());
factory.getEngineByName("nashorn").eval("ftp.upload('')");