Java uses the scripting engine to execute JS

Source: Internet
Author: User

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 (); }/** * Function in JS object * * @return */public static object Jsobjfunc () {String script = "var obj={run:fu Nction () {return ' run method:return:\ ' abc ' +this.next (' test ') + ' \ ';},next:function (str) {return ' I come to next function ' +    Str+ '}} ";    Scriptenginemanager sem = new Scriptenginemanager ();    ScriptEngine ScriptEngine = Sem.getenginebyname ("JS");      try {scriptengine.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 JS Object Numeric Type Attribute * * @return */public static object[] GetArray () {Scriptenginemanager sem = new Script    Enginemanager ();    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 calculation * * @param script * @return */public static Object Jscalculate (String script) {ScriptEngine    Manager Manager = new Scriptenginemanager ();    ScriptEngine engine = Manager.getenginebyname ("javascript"); try {return (Object) engine.eval (script);    } catch (Scriptexception ex) {ex.printstacktrace ();  } return null;    }/** * Run 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 ();    }}/** * JS in variable 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 (); }  }}

Execute the main function to return the result as follows

Run Method:return: "ABC I came to next function test)"

[Test, True, 1.0, 1.0, 2.11111]

10.0

Hello,test

Path:/data/js.txt


Java uses the scripting engine to execute JS

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.