Program/ Example of Nashorn Javascript (js) Engine in Java 8 - Print location and directory of javascript file >
package nashornJs;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
*
* Print location and directory of javascript file
*
*/
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
Print location of javascript file = file:/E:/pocJmse/nashornJs/myNashornJavaScript.js
Print current line number of javascript file = 4
Print current working directory = E:\pocJmse\nashornJs\
End myNashornJavaScript.js
In java - FINISH
*/
|
E:/pocJmse/myNashornJavaScript.js looks like this >
print('In myNashornJavaScript.js');
print("Print location of javascript file = "+ __FILE__);
print("Print current line number of javascript file = "+ __LINE__);
print("Print current working directory = "+ __DIR__);
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.