Nashorn is the operating environment for efficient and dynamic execution of JS code in Java.
New Scriptenginemanager (). Getenginebyname ("Nashorn"); Engine.eval (// execute JS from source new File ("Resources/scripts.js"); Engine.eval (new// Execute JS from file
Call JS
First, save the JS code that needs to be called as Scripts.js:
function Add (A, b) { return a + b;} var function (name) { print ("JS:" + name); return "Greeting from JS";} var function (obj) { print ("JS Class:" + Object.prototype.toString.call (obj));}
Called in Java:
ScriptEngine engine =NewScriptenginemanager (). Getenginebyname ("Nashorn"); Engine.eval ("Print (' Hello ');");//Execute JS from sourceFile f =NewFile ("Resources/scripts.js"); Engine.eval (NewFileReader (f));//Execute JS from file invocable invocable = (invocable) engine; Object sum=invocable.invokefunction("Add", 1, 2); SYSTEM.OUT.PRINTLN (sum); Invocable.invokefunction ("F2",NewDate ()); Invocable.invokefunction ("F2", Localdatetime.now ());
Because the code in JS is the original ecological call in the JVM, the type is not lost.
Calling Java
The Java method must be public . Otherwise, it cannot be found at the time of the call :
Public Static string Greeting (string name) { System.out.println ("from Java" + name); return "From Java"; } Public Static void getcls (Object obj) { System.out.println (Obj.getclass ()); }
ScriptEngine engine = new Scriptenginemanager (). Getenginebyname ("Nashorn");
File F = new file ("Resources/scripts.js");
Engine.eval (new FileReader (f)); Execute JS from File
JS Code:
var cls = Java.type ("Nashorn". Nashorntest ");p rint (cls.greeting (' Zhijie '));
The original JS type can be converted into the appropriate Java wrapper class. A native JS object is represented as an internal adaptation class.
CLS.GETCLS (123);//class Java.lang.IntegerCls.getcls (49.99);//class Java.lang.DoubleCls.getcls (true);//class Java.lang.BooleanCls.getcls ("Hi There")//class Java.lang.StringCls.getcls (NewNumber (23));//class Jdk.nashorn.internal.objects.NativeNumberCls.getcls (NewDate ());//class Jdk.nashorn.internal.objects.NativeDateCls.getcls (NewRegExp ());//class Jdk.nashorn.internal.objects.NativeRegExpcls.getcls ({foo:' Bar '});//class Jdk.nashorn.internal.scripts.JO4
Scriptobjectmirror
Jdk.nashorn.api.scripting.ScriptObjectMirror is the mapping of JS objects in Java. This class implements the map interface.
public static void F ( scriptobjectmirror Mirror) {System.out.println (mirr Or. GetClassName () + ":" + arrays.tostring (mirror. Getownkeys ( true public static void Callmethodofjsobject (scriptobjectmirror person) {System.out.println ( span> "Full Name was:" + person. Callmember ("Getfullname"
CLS.F ({A: ' A ', B: ' B '}); function Person (FirstName, lastName) { this. firstName = firstName; this. lastName = lastName; This function () { returnthis. lastName; }} var New Person ("Zhijie", "Liu"); Cls.callmethodofjsobject (Person1);
Nashorn expanded the ECMAScript 5:
Can read further http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/
NASHORN-JS engine