运行 "Graphviz" viz.js 来自 Java 8 Nashorn 引擎

Running "Graphviz" viz.js from Java 8 Nashorn Engine

我正在尝试从服务器上的 Java 8(Nashorn 引擎)渲染 graphviz,但出现错误:

Exception in thread "main" javax.script.ScriptException: ReferenceError: "Promise" is not defined in <eval> at line number 120
    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:455)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:387)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeMethod(NashornScriptEngine.java:196)
    at test1.VizDemo.main(VizDemo.java:24)
Caused by: <eval>:120 ReferenceError: "Promise" is not defined

到目前为止,这是我的(简单)程序:

import java.io.InputStreamReader;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class VizDemo {

  public static void main(String args[]) throws Exception {

    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");

    engine.eval(new InputStreamReader(VizDemo.class.getResourceAsStream(
      "./viz.js")));
    engine.eval(new InputStreamReader(VizDemo.class.getResourceAsStream(
      "./full.render.js")));

    Object v = engine.eval("new Viz();");
    System.out.println("v=" + v);

    if (engine instanceof Invocable) {
      Invocable invEngine = (Invocable) engine;
      Object result = invEngine.invokeMethod(v, "renderSVGElement",
          "digraph { a -> b; a -> c; }");
      System.out.println("result=" + result);
    }
  }

}

我错过了什么?或者...实际上可以 运行 Java 在 Java 8 中编写脚本吗?

Nashorn 不支持 Promise,您需要实施 "Pollyfill" 以提供 Promise 支持。这是一个实现:https://github.com/shendepu/nashorn-polyfill