Java code executes arithmetic operations and js Code.

Source: Internet
Author: User

Java code executes arithmetic operations and js Code.

Java SE6 supports common scripting languages.

You can execute the script language in java code, use get ("key"), put ("key", "value") to interact with it, and dynamically call the script.

Package com. test; import java. util. list; import javax. script. invocable; import javax. script. scriptEngine; import javax. script. scriptEngineFactory; import javax. script. scriptEngineManager; import javax. script. scriptException; public class Demo {public static void main (String [] args) {ScriptEngineManager manager = new ScriptEngineManager (); // obtain the List of all engine factories <ScriptEngineFactory> list = manager. getEngineFactories (); for (ScriptEngineFactory factory: list) {System. out. println ("supported scripting language" + factory. getNames (); // supported scripting languages [js, rhino, JavaScript, javascript, ECMAScript, ecmascript]} // get common javascript scripts ScriptEngine engine = manager. getEngineByName ("javascript"); try {// execute the script System. out. println (engine. eval ("2 + 3*2"); // 8 System. out. println (engine. eval ("(true | false) & true"); // true // interacts with the script engine. put ("name", "123456"); engine. eval ("var out =''; "+" for (I = 0; I <name. length; I ++) {"+" out = name. charAt (I) + out} "); String name = (String) engine. get ("out"); System. out. println ("name:" + name); // dynamically call the script language (the Invocable interface must be implemented by the script engine) String str = "abcdef"; if (engine instanceof Invocable) {engine. eval ("function str1 (name) {" + "var out =''; "+" for (I = 0; I <name. length; I ++) {"+" out = name. charAt (I) + out} "+" return out;} "+" function str2 (name) {"+" return 123} "); Invocable invocable = (Invocable) engine; try {Object obj = invocable. invokeFunction ("str1", "123456"); System. out. println ("obj:" + obj);} catch (NoSuchMethodException e) {e. printStackTrace () ;}// dynamic implementation Interface engine. eval ("function run () {" + "print ('asynchronous call')}"); Invocable invocable = (Invocable) engine; // implement the Runnable interface Runnable runnable = invocable. getInterface (Runnable. class); // start Thread t = new Thread (runnable); t. start ();} catch (ScriptException e) {e. printStackTrace ();}}}

 

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.