Execute JavaScript file

suggest change
// Required imports
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.FileReader;
import java.io.FileNotFoundException;

// Obtain an instance of the JavaScript engine
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("nashorn");

// Load and execute a script from the file 'demo.js'
try {
    engine.eval(new FileReader("demo.js"));
} catch (FileNotFoundException ex) {
} catch (ScriptException ex) {
    // This is the generic Exception subclass for the Scripting API

// Outcome:
// 'Script from file!' printed on standard output


print('Script from file!');

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents