Create demo1.js file
print('Demo 1');
Invoking Javascript Functions from Java Without Parameters
package pmk.learnjava8withrealapps.nashorn;
import java.io.FileReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class Demo {
public static void main(String[] args) {
try {
ScriptEngine se = new ScriptEngineManager()
.getEngineByName("nashorn");
se.eval("print('Hello JS');");
se.eval(new FileReader("src\\pmk\\learnjava8withrealapps\\nashorn\\demo1.js"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Output
Hello JS
Demo 1