JS 脚本不 运行 与 Java 11 但适用于 Java 8

JS script doesn't run with Java 11 but works with Java 8

我有一个 JS 脚本在 运行 使用 JDK 8 时工作正常,但在 Java 11.

时失败并出现错误

错误是:

 unknown call type GET:PROPERTY|ELEMENT|METHOD:NODE_PATH(Object)int@jdk.nashorn.internal.scripts.Script$Recompilation678$\^eval\_
 

Java代码:

ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(evalCode);
Invocable invocable = (Invocable) engine;
invocable.invokeFunction("__toCall");   

Java脚本代码:

function __toCall() {
    return require('./src/main/resources/ontology/nashorn-invoker')(
        function (require) {
            print('in nashornInvoker: require=' + require);
            var Text = function Text() {
                StringLeaf.call(this);
            };
            Text.prototype.ontologyType = function () {
                return 'cmd.ontology.types.Text';
            };

            var BasicTest = function BasicTest() {
                var _field = new Text();
                _field._fieldName = 'field';
                _field._name = 'Field';
                _field._defaultValue = '';

                var curNode = this;
                Structure.call(this, {
                    field: _field
                });
            };
            BasicTest.prototype.ontologyType = function () {
                return 'cmd.ontology.test.BasicTest';
            };
            BasicTest.prototype._name = 'Basic Test';
            return (function () {
                var model = [];
                var callbackFn = function (result) {
                    if (result !== true) model.push.apply(model, result);
                };
                test.child('field').validate(vc, callbackFn);
                var result = model.length === 0 ? true : model;
                print('Callback function result: ' + JSON.stringify(result));
                return result;
            })();
        }
    );
     }

这听起来像是 JDK-8261926 bug. The good news is that this is fixed, but only in the standalone Nashorn, not the one integrated into Java 11. There's also a page 描述了如何确保程序使用独立 Nashorn 而不是 Java 11 上的集成 Nashorn。