create Array in Nashorn Javascript (js) Engine in Java 8




Program/ Example of Nashorn Javascript (js) Engine in Java 8 - create Array in javascript >

package nashornJs;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
*
* create Array in javascript
*
*/
public class MyNashornClass {
   public static void main(String[] args) throws Exception {
      System.out.println("In java - START");
      
      ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
       engine.eval("load('E:/pocJmse/myNashornJavaScript.js')");    
       System.out.println("In java - FINISH");
      
   }
}
/* OUTPUT
In java - START
In myNashornJavaScript.js

Step 1 - create myArray in javascript >

Step 2 - Display myArray using for each loop in javascript >
1
2
3

Step 3 - Display myArray using for loop in javascript >
1
2
3
End myNashornJavaScript.js
In java - FINISH


*/



E:/pocJmse/myNashornJavaScript.js looks like this >
print('In myNashornJavaScript.js');
print('\nStep 1 - create myArray in javascript >');
var myArray = [1, 2, 3];
print('\nStep 2 - Display myArray using for each loop in javascript >');
for each (var i in myArray)
   print(i);
print('\nStep 3 - Display myArray using for loop in javascript >');
for (i = 0 ; i < myArray.length ;i++)
   print(myArray[i]);
print('End myNashornJavaScript.js');




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

eEdit
Must read for you :