use GraalVM. The reason is that it enable JS-Java interoperability.
you have to download the graalVM via adding as dependency in maven (that's if you use maven build).
Then do something like the following to use it:
ScriptEngine scriptEngine = new
ScriptEngineManager().getEngineByName("javascript");
Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("polyglot.js.allowHostAccess", true);
bindings.put("polyglot.js.allowHostClassLookup", (Predicate<String>)
YourClass -> true);
scriptEngine.eval("print('hello world')");
scriptEngine.eval("var page = Packages.YourPackage(e.g.:
com.payments.weeklypayments).YourClass;"+ "print('hello world')");
For external js file:
scriptEngine.eval(new FileReader("src/functparam.js""));
Then in the 'functparam.js', do something like the following:
var sumOfTwoNumbers = function(){
var variableName = Packages.com.payments.weeklypayments.YourClass;
print('summing method from java: '+ variableName.summing());
}
Meanwhile, in YourClass.java, you should have a method 'summing()'
@HostAccess.Export
public static int summing(){
return 112;
}
So, to pass in parameters: you can add params into the method like
the following:
@HostAccess.Export
public static int summing(int a, int b){
return a+b;
}
And then, from 'functparam.js', do something like the following:
var sumOfTwoNumbers = function(){
var variableName =
Packages.com.payments.weeklypayments.YourClass;
print ('summing method from java: '+ variableName.summing(3+7));
}