Run Javascript function with ScriptEngine
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class Main { public static void main(String[] argv) throws Exception { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); String myJSCode = "function myFunction(){return (40 + 2);}myFunction();"; System.out.println(engine.eval(myJSCode)); } }