JAVA uses the script engine to execute JS and java Script Engine js

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.