Call java methods from javascript (js) file to pass JSON in java and print all keys and values of passed json - Use ScriptObjectMirror - in Nashorn Javascript (js) Engine in Java 8




Program/ Example of Nashorn Javascript (js) Engine in Java 8 - call java methods from javascript (js) file - call java method from javascript to pass JSON in java and print all keys and values of passed json - Use ScriptObjectMirror >  


package nashorn10_CallJavaMethodsFromJs2;
import java.util.Arrays;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
/**
* How to call java methods from javascript (js) file
* Pass json from javascript to java
* Use jdk.nashorn.api.scripting.ScriptObjectMirror class to store json in java
*/
public class CallJavaMethodsFromJs {
   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");
   }

   /**
      This method will be called from Javascript (js) -
      This method print all keys and values of passed json
      Use jdk.nashorn.api.scripting.ScriptObjectMirror class to store json in java
   */
   public static void printJsonKeyAndValueMethodInJava(ScriptObjectMirror jsonScriptObjectMirror) {
      System.out.println("In java - In printJsonKeyAndValue method");
      // Get all the keys in scriptObjectMirror
      String stringArray[] = jsonScriptObjectMirror.getOwnKeys(true);
      // Display stringArray[]
      System.out.print("In java - Display all keys in json > ");
      System.out.println(Arrays.toString(stringArray));
      System.out.println("\n"+"In java - Display all values corresponding to keys in json > ");
      System.out.println("name = "+ jsonScriptObjectMirror.getMember("name")); //Or
                                               // scriptObjectMirror.getMember(stringArray[0])
      System.out.println("city = "+ jsonScriptObjectMirror.getMember("city")); //Or
                                               // scriptObjectMirror.getMember(stringArray[1])
   }
}
/*OUTPUT
In java - START
In myNashornJavaScript.js
In js - create json
In js - passing json to java
In java - In printJsonKeyAndValue method
In java - Display all keys in json > [name, city]
In java - Display all values corresponding to keys in json >
name = Ankit
city = Delhi
End myNashornJavaScript.js
In java - FINISH
*/


E:/pocJmse/myNashornJavaScript.js looks like this >
print('In myNashornJavaScript.js');
var CallJavaMethodsFromJs = Java.type('nashorn10_CallJavaMethodsFromJs2.CallJavaMethodsFromJs');
print('In js - create json');
var myJson = {   name : 'Ankit',  city : 'Delhi'};
print('In js - passing json to java');
CallJavaMethodsFromJs.printJsonKeyAndValueMethodInJava(myJson);
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 :