Java ScriptEngineFactory get output statement

Description

Java ScriptEngineFactory get output statement


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

public class Main {
  public static void main(String[] args) {
    // Get the script engine factory for Nashorn
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    ScriptEngineFactory factory = engine.getFactory();

    // Get the script
    String script = factory.getOutputStatement("\" 1 + 2 \"");
    System.out.println("Syntax: " + script);

    // Evaluate the script
    try {/* w  w  w  .j  av  a  2s.c  o m*/
      engine.eval(script);
    } catch (ScriptException e) {
      e.printStackTrace();
    }
  }
}



PreviousNext

Related