Java 7 runable Nashorn instead of Rhino
Someone has dumped Nashorn dump on OpenJDK so that Java 7 can be used!
Nashorn was originally available in Java 8. Now someone is backward compatible. Good thing!
Compile source code
Only the source code does not have jar, so you need to compile it yourself. It's easy: ant-f make/build. xml. First, drag the source code to the Eclipse project, and then open the Ant View:
Click the + icon to add make/build. xml
Then, "execute" to compile the jar package and save it in the dist directory.
Test
Test availability:
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());}}
Try {final Class cls = Class. forName ("jdk. nashorn. api. scripting. ScriptObjectMirror ");}..
Comparison of Rhino
Create an 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 encapsulated api is easy to use, for example:
Map s = n. eval ("g = {a: 1};", Map. class); // converts a js object to a 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 Observation found:
The {} hash type of js is automatically converted to jdk. nashorn. api. scripting. ScriptObjectMirror instead of NativeObject of Rhino, but both can be converted to Map.
The [] array type of js is automatically converted to jdk. nashorn. api. scripting. ScriptObjectMirror instead of NativeArray of Rhino. However, you can use isArray (): boolean to determine whether the array is used.
The Number type of js will be automatically converted to java. lang. Integer instead of the Double type of Rhino, which makes it easier to process the Number type.
However, this is an earlier version and lacks formal functions, such:
If (so. isArray () {int [] iarr = (int []) ScriptUtils. convert (so, int []. class); // convert to a java array for saving, because no convert ()}
What should I do? Think of a solution.
public static void main(String[] args) throws ScriptException, IOException {Nashorn n = new Nashorn();n.load("C:/project/spring-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 scriptObjectMirror) {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
entry : scriptObjectMirror.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, if you are not obsessive-compulsive disorder, the array get (0)/get (1)/... is also available and does not need to be converted once.