Java8 new JavaScript scripting engine Nashorn Small test

Source: Internet
Author: User

A very detailed description of the Nashorn scripting engine: http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/


Here is a small example of my test that simulates the scripting of damage calculations in the game:


Scripttest.java:

Package com.zl1030.scripttest;import java.io.filereader;import javax.script.invocable;import  javax.script.scriptengine;import javax.script.scriptenginemanager;public class scripttest  {    public static void main (String[] args)  {         try {             scriptenginemanager factory = new scriptenginemanager ();             ScriptEngine engine =  Factory.getenginebyname ("Nashorn");             String scriptpath = system.getproperty ("User.dir")  +  "/scripts/test1.js";             engine.eval (New filereader (ScriptPath));             invocable invocable =  (invocable)  engine;             test1 test1 = invocable.getinterface ( Test1.class);             scriptresult result  =  (Scriptresult)  test1.calc (New a (),  new a ());             system.out.println (Test1.getlength ("ABCDEFG"));             system.out.println (Result.getdamage ()  +  "   " + result.gettarget (). Geta ()  + "   " + result.isresult ());         } catch  (exception e)  {             e.printstacktrace ();         }    }}


Test1.java:

Package Com.zl1030.scripttest;public interface Test1 {public int getlength (String msg);    Public Scriptresult Calc (a A, a b); }

Scriptresult.java:

Package com.zl1030.scripttest;public class scriptresult {    private  a target;    private boolean result;    private  int damage;    public a gettarget ()  {         return target;    }    public void  settarget (A target)  {        this.target =  Target;    }    public boolean isresult ()  {         return result;    }    public  void setresult (Boolean result)  {         This.result = result;    }    public int getdamage ()  {   &Nbsp;    return damage;    }    public  Void setdamage (int damage)  {        this.damage =  damage;    }}

A.java:

Package Com.zl1030.scripttest;public Class A {private int A = 0;    private int b = 0;    public int Geta () {return A;    } public void SetA (int a) {this.a = A;    } public int Getb () {return b;    } public void Setb (int b) {this.b = b; }}

Test1.js:

var scriptresultclass=java.type (' Com.zl1030.ScriptTest.ScriptResult '); function GetLength (msg) {return msg.length;}    Function Calc (A, b) {A.seta (1);    A.SETB (2);    var result = new Scriptresultclass ();    Result.settarget (a);    Result.setdamage (10);    Result.setresult (TRUE); return result;}


This article is from the "zl1030 Records" blog, so be sure to keep this source http://zl1030.blog.51cto.com/274507/1660007

Java8 new JavaScript scripting engine Nashorn Small test

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.