Call java script - Then Create and access java.util.Date in javascript in Java 8




Program/ Example of Nashorn Javascript (js) Engine in Java 8 - Call java script - Then Create and access java.util.Date in javascript >

package nashorn4_date;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* Call java script - Then Create and access java Date in javascript
*
*/
public class Nashorn_useJavaObjects_Date_InJavascript {
   public static void main(String[] args) throws Exception {
          ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
          System.out.println("In java - START");
          // Call js (javascript) file
      engine.eval("load('E:/pocJmse/myNashornJavaScript.js')");
          System.out.println("In java - FINISH");
   }
}
/* OUTPUT
In java - START
In myNashornScript.js
Step 1 - Create java Date object in javascript
Step 2 - Initialize java Date object
Step 3 - Display current Date
Sat Feb 18 20:33:07 IST 2017
End myNashornScript.js
In java - FINISH
*/


E:/pocJmse/myNashornJavaScript.js looks like this >
print('In myNashornScript.js');
print('Step 1 - Create java Date object in javascript');
var javaDate = Java.type('java.util.Date');
print('\nStep 2 - Initialize java Date object');
var myJavaDate = new javaDate();
print('\nStep 3 - Display current Date');
print(myJavaDate);
print('\nEnd myNashornScript.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 :