Call java methods from javascript (js) file to print object value and its type 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 print object value and its type in java >


package nashorn10_CallJavaMethodsFromJs1;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* How to call java methods from javascript (js) file
*
*/
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 object value and its type
   */
   public static void printObjectValueAndTypeMethodInJava(Object object) {
       System.out.println("In java - object value = "+object+", object type = "        
                                                                  +object.getClass());
   }
}
/*OUTPUT
In java - START
In myNashornJavaScript.js

In js -   object value = Hello, object type = string
In java - object value = Hello, object type = class java.lang.String

In js -   object value = Ankit, object type = object
In java - object value = Ankit, object type = class jdk.nashorn.internal.objects.NativeString

In js -   object value = 11, object type = number
In java - object value = 11, object type = class java.lang.Integer

In js -   object value = 12.1, object type = number
In java - object value = 12.1, object type = class java.lang.Double

In js -   object value = 13, object type = object
In java - object value = 13.0, object type = class jdk.nashorn.internal.objects.NativeNumber

In js -   object value = Sun Feb 19 2017 17:54:39 GMT+0530 (IST), object type = object
In java - object value = Sun Feb 19 2017 17:54:39 GMT+0530 (IST), object type = class jdk.nashorn.internal.objects.NativeDate

In js -   object value = true, object type = boolean
In java - object value = true, object type = class java.lang.Boolean

In js -   object value = //* /, object type = object
In java - object value = //* /, object type = class jdk.nashorn.internal.objects.NativeRegExp
End myNashornJavaScript.js
In java - FINISH
*/

E:/pocJmse/myNashornJavaScript.js looks like this >
print('In myNashornJavaScript.js');
var CallJavaMethodsFromJs = Java.type('nashorn10_CallJavaMethodsFromJs1.CallJavaMethodsFromJs');
//First, print type of object in javascript
//Then Call java method to print object value and its type in java
var str="Hello"; //string in js,
                //java.lang.String class in java
print ("\n"+"In js -   object value = "+str +", object type = "+typeof str)
CallJavaMethodsFromJs.printObjectValueAndTypeMethodInJava(str);
var str1=new String("Ankit"); //string in js,
                            //java.lang.String class in java
print ("\n"+"In js -   object value = "+str1 +", object type = "+typeof str1)
CallJavaMethodsFromJs.printObjectValueAndTypeMethodInJava(str1);
var i = 11; //number in js,
          //java.lang.Integer class in java
print ("\n"+"In js -   object value = "+i +", object type = "+typeof i)
CallJavaMethodsFromJs.printObjectValueAndTypeMethodInJava(i);
var dou = 12.1; //number in js,
               //java.lang.Double class in java
print ("\n"+"In js -   object value = "+dou +", object type = "+typeof dou)
CallJavaMethodsFromJs.printObjectValueAndTypeMethodInJava(dou);
var num = new Number(13); //object in js,
          //java.lang.jdk.nashorn.internal.objects.NativeNumber class in java
print ("\n"+"In js -   object value = "+num +", object type = "+typeof num)
CallJavaMethodsFromJs.printObjectValueAndTypeMethodInJava(num);
var dat = new Date(); //object in js,
             //java.lang.jdk.nashorn.internal.objects.NativeDate class in java
print ("\n"+"In js -   object value = "+dat +", object type = "+typeof dat)
CallJavaMethodsFromJs.printObjectValueAndTypeMethodInJava(dat);
var bool = true; //boolean in js,
                //java.lang.Boolean in java
print ("\n"+"In js -   object value = "+bool +", object type = "+typeof bool)
CallJavaMethodsFromJs.printObjectValueAndTypeMethodInJava(bool);
var regularExpression = new RegExp("/*"); //object in js,
                     //jdk.nashorn.internal.objects.NativeRegExp class in java
print ("\n"+"In js -   object value = "+regularExpression +", object type = "+typeof regularExpression)
CallJavaMethodsFromJs.printObjectValueAndTypeMethodInJava(regularExpression);
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 :