yes, It is possible to call javascript from java, and the functionality is built right into the JVM!.
Here’s a simple way to do it . Lets say you have a javascript called file.js, which has a method called sum(a,b). To call this method from java, do the following:
The imports:
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
Creating the Engine:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(“JavaScript”);
Reading the javascript file or write the actual javascript inside engine.eval
engine.eval(new FileReader(new File(“file.js”)));
Invocable inv = (Invocable) engine;
The method:
call the sum method with arguments a=2 and b=3.
inv.invokeFunction(“sum”,2,3)