JDK8 dynamically define and run code in the Java Transfer JavaScript scripting engine

Source: Internet
Author: User

Import Java.lang.*;import java.util.arrays;import Java.util.list;import Javax.script.invocable;import Javax.script.scriptengine;import Javax.script.scriptenginemanager;public class Scriptenginetest {public static void    Main (string[] args) throws Exception {Scriptenginemanager sem = new Scriptenginemanager ();     ScriptEngine engine = Sem.getenginebyname ("javascript");   Python or Jython, <pre name= "code" class= "HTML" >//into the context of the variable engine.put ("msg", "Just a Test"); Defines the class user String str = "MSG + = '!!! '; var user = {name: ' Tom ', age:23,hobbies:[' football ', ' Basketball ']};   ";   Engine.eval (str);   Takes a value from the context engine String msg = (string) engine.get ("msg");   String name = (string) engine.get ("name");   string[] HB = Engine.get ("HB");   SYSTEM.OUT.PRINTLN (msg);   SYSTEM.OUT.PRINTLN (name + ":" + hb[0]); Defines the mathematical function engine.eval ("function add (A, b) {c = a + B; return C;    }");  Get the Calling interface invocable Jsinvoke = (invocable) engine; Define an addition function Object RESULT1 = jsinvoke.invokefunction ("Add", new object[] {10, 5});  System.out.println (RESULT1);  Call the addition function, pay attention to the method of Adder Adder = Jsinvoke.getinterface (Adder.class);  int result2 = Adder.add (10, 35);  System.out.println (RESULT2);  Define the Run () function Engine.eval ("function run () {print (' www.java2s.com ');}");  Invocable invokeengine = (invocable) engine;  Runnable runner = Invokeengine.getinterface (Runnable.class);  Thread t = new Thread (runner) that defines threading execution;  T.start ();  T.join ();  Import other Java packages String Jscode = "Importpackage (java.util); var list2 = arrays.aslist ([' A ', ' B ', ' C ']);  ";  Engine.eval (Jscode);  List<string> List2 = (list<string>) engine.get ("List2");  for (String val:list2) {System.out.println (val);} }}



The scripting engine provides good extensibility for implementing dynamic functions, such as plug-in mechanisms.

Documentation for the engine interface:

ScriptEngine

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

JDK8 dynamically define and run code in the Java Transfer JavaScript scripting engine

Related Article

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.