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