JAVA uses the script engine to execute JS and java Script Engine js
Import java. io. file; import java. lang. reflect. field; import java. util. arrays; import javax. script. invocable; import javax. script. scriptEngine; import javax. script. scriptEngineManager; import javax. script. scriptException; public class JavaExecScriptDemo {public static void main (String [] args) {System. out. println (jsObjFunc (); System. out. println (Arrays. toString (getArray (); System. out. println (jsCalculate ("A = 1 + 2 + 3 + (2*2)"); jsFunction (); jsVariables ();} /*** run the function ** @ return */public static Object jsObjFunc () {String script = "var obj = {run: function () {return 'run method: return: \ "abc '+ this. next ('test') + '\ "';}, next: function (str) {return 'I am going to next function' + str + ')'}}"; scriptEngineManager sem = new ScriptEngineManager (); ScriptEngine scriptEngine = sem. getEngineByName ("js"); try {scriptEn Gine. eval (script); Object object = scriptEngine. get ("obj"); Invocable inv2 = (Invocable) scriptEngine; return (String) inv2.invokeMethod (object, "run");} catch (Exception e) {e. printStackTrace ();} return null;}/*** get the numeric type attribute of the js Object ** @ return */public static Object [] getArray () {ScriptEngineManager sem = new ScriptEngineManager (); String script = "var obj = {array: ['test', true, 1, 1.0, 2.11111]}"; ScriptEngine scriptEngine = sem. getEngineByName ("js"); try {scriptEngine. eval (script); Object object2 = scriptEngine. eval ("obj. array"); Class <?> Clazz = object2.getClass (); Field denseField = clazz. getDeclaredField ("dense"); denseField. setAccessible (true); return (Object []) denseField. get (object2);} catch (Exception e) {e. printStackTrace ();} return null;}/*** JS computing ** @ param script * @ return */public static Object jsCalculate (String script) {ScriptEngineManager manager = new ScriptEngineManager (); ScriptEngine engine = manager. getEngineByName ("javascript"); try {return (Object) engine. eval (script);} catch (ScriptException ex) {ex. printStackTrace ();} return null;}/*** run the JS basic function */public static void jsFunction () {ScriptEngineManager sem = new ScriptEngineManager (); ScriptEngine se = sem. getEngineByName ("javascript"); try {String script = "function say (name) {return 'hello, '+ name;}"; se. eval (script); Invocable inv2 = (Invocable) se; String res = (String) inv2.invokeFunction ("say", "test"); System. out. println (res);} catch (Exception e) {e. printStackTrace () ;}}/*** variables in JS use */public static void jsVariables () {ScriptEngineManager sem = new ScriptEngineManager (); ScriptEngine engine = sem. getEngineByName ("javascript"); File file = new File ("/data/js.txt"); engine. put ("file", file); try {engine. eval ("println ('path: '+ file. getAbsoluteFile () ");} catch (ScriptException e) {e. printStackTrace ();}}}
The result returned by executing the Main function is as follows:
Run method: return: "abc: next function test )"
[Test, true, 1.0, 1.0, 2.11111]
10.0
Hello, test
Path:/data/js.txt