Loading and Executing external JavaScript from Java - Java Scripting

Java examples for Scripting:Run Script

Description

Loading and Executing external JavaScript from Java

Demo Code

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Main {

  public static void main(String[] args) {

    ScriptEngineManager sem = new ScriptEngineManager();
    ScriptEngine nashorn = sem.getEngineByName("nashorn");

    try {/*ww w.  j  a v a  2 s  .c  om*/
      nashorn.eval("load('helloNashorn.js')");
    } catch (ScriptException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
}

Result


Related Tutorials