Java ScriptEngine class

Description

Java ScriptEngine class


import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Main {
  public static void main(String[] args) {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    if (engine == null) {
      System.out.println("JavaScript engine is not available. ");

      return;/*w w  w.j a v  a2s .  co  m*/
    }
    // Use simple expressions with numeric literals 
    String script = "print('Hello Scripting!')"; 
    Object result;
    try {
      result = engine.eval(script);
      System.out.println(script + " = " + result); 
    } catch (ScriptException e) {
      e.printStackTrace();
    } 
    
  }
}



PreviousNext

Related