Java 7 executable Nashorn, instead of Rhino

Source: Internet
Author: User

Surprise the existing people on the OpenJDK on the Nashorn dump down, so that Java 7 can be used. The source code is in https://bitbucket.org/ramonza/nashorn-backport/.

Originally Nashorn is Java 8 only. Now someone is backwards compatible. Good!

Compiling source code

Only the source code does not have a jar. To compile it yourself. It doesn't matter very easy:ant-f make/build.xml. The detailed steps first drag the source code into the Eclipse project. Then open the Ant view:


Click the + icon to add Make/build.xml


Then "Run" to compile the jar package, and then save it in the Dist folder.

Suppose you can't compile successfully, give everyone a direct: http://download.csdn.net/detail/zhangxin09/9398572

Test

Test is available:

Import javax.script.*; public class Nashorntest {public static void main (String args[]) {Scriptenginemanager manager = new Scriptenginemanager (); For (Scriptenginefactory f:manager.getenginefactories ()) {printbasicinfo (f); System.out.println ();} ScriptEngine Nashorn = Manager.getenginebyname ("Nashorn"), if (Nashorn! = null) {System.out.println ("Nashorn is present."); }else {System.out.println ("Nashorn is not present.");} public static void Printbasicinfo (Scriptenginefactory factory) {System.out.println ("engine name=" + Factory.getenginename ()); System.out.println ("engine version=" + factory.getengineversion ()); System.out.println ("Language name=" + factory.getlanguagename ()); System.out.println ("extensions=" + factory.getextensions ()); System.out.println ("Language version=" + factory.getlanguageversion ()); System.out.println ("names=" + factory.getnames ()); System.out.println ("MIME types=" + factory.getmimetypes ());}}

Another way to check if it is available: try{final class<?> cls = Class.forName ("Jdk.nashorn.api.scripting.ScriptObjectMirror");

Compare Rhino

Create an already encapsulated JS VM

Nashorn n = new Nashorn (); Object s = N.eval ("g={a:1};"); Map SS = (map) s;ss.get ("a"); System.out.println (Ss.get ("a"). GetClass (). GetName ()); System.out.println (S.getclass (). GetName ());

The API I encapsulated feels more comfortable, such as:

Map s = N.eval ("g={a:1};", Map.class); JS Object converted to Java map

        Nashorn n = new Nashorn (), Object obj = N.eval ("G=[1, 2, 3];"); System.out.println (Obj.getclass (). GetName ()); Scriptobjectmirror so = (scriptobjectmirror) obj; System.out.println (So.get (0). GetClass (). GetName ());
Test observations revealed that:

The {} hash type of JS will be actively converted to jdk.nashorn.api.scripting.ScriptObjectMirror. Rather than Rhino's nativeobject. But both can switch to MAP.

JS's [] array type will take its own initiative to Jdk.nashorn.api.scripting.ScriptObjectMirror, not Rhino's nativearray, but be able to use IsArray (): Boolean to infer whether an array

JS number type will take the initiative to convert to Java.lang.Integer. Instead of a Double in Rhino, this is more convenient when dealing with numeric types.

Just this is the earlier version number. Lack of a formal version of the function. Like what:

if (So.isarray ()) {  int[] Iarr = (int[]) Scriptutils.convert (So, int[].class);//Convert to Java array save. Because there is no convert ()}

What can I do but what will we do? Think of a way to do (in fact, Google a bit).

public static void Main (string[] args) throws Scriptexception, IOException {nashorn n = new Nashorn (); N.load ("c:/project/s Pring-test/src/com/ajaxjs/framework/config.js "), Object obj = N.eval (" G=[1, 2, 3]; "); System.out.println (Obj.getclass (). GetName ()); Scriptobjectmirror so = (scriptobjectmirror) obj; System.out.println (So.get (0). GetClass (). GetName ()); if (So.isarray ()) {System.out.println (so);//int[] Iarr = (int[]) Scriptutils.convert (So, int[].class);}} /** * js arr2 java arr * @param scriptobjectmirror * @return */public static object[] ToArray (scriptobjectmirror scriptobj Ectmirror) {if (!scriptobjectmirror.isarray ()) {throw new IllegalArgumentException ("Scriptobjectmirror is no Array");} if (Scriptobjectmirror.isempty ()) {return new object[0];} object[] Array = new Object[scriptobjectmirror.size ()];int i = 0;for (map.entry<string, object> entry:scriptobject Mirror.entryset ()) {Object result = Entry.getvalue (); if (Result instanceof scriptobjectmirror && Scriptobjectmirror.isArray ()) {Array[i] = ToArray ((scriptobjectmirror) result);} else {array[i] = result;} i++;} return array;}

In fact, suppose you're not obsessive-compulsive. Array get (0)/get (1)/... is available without conversion.

Single-gauge code (very important!)

) Http://code.taobao.org/p/bigfoot_v2/src/java_v3/test/javascript/TestJS.java

Nashorn Document: http://cr.openjdk.java.net/~sundar/jdk.nashorn.api/8u20/javadoc/jdk/nashorn/api/scripting/AbstractJSObject.html

Java 7 executable Nashorn, instead of Rhino

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.