Search code examples
javajava-8java-11nashorn

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


I have a JS script that works fine when run using JDK 8, but fails with the error on Java 11.

The error is:

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

Java Code:

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

Javascript code:

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;
            })();
        }
    );
     }

Solution

  • This sounds like it could be the 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 describing how you can ensure that standalone Nashorn is used by your program instead of the integrated one on Java 11.