Switching from Rhino to Nashorn

前端 未结 7 934
死守一世寂寞
死守一世寂寞 2020-12-13 13:46

I have a Java 7 project which makes a lot of use of Javascript for scripting various features. Until now I was using Rhino as script engine. I would now like to move to Java

7条回答
  •  爱一瞬间的悲伤
    2020-12-13 14:15

    One problem is that Nashorn can no longer by default import whole Java packages into the global scope by using importPackage(com.organization.project.package);

    There is, however, a simple workaround: By adding this line to your script, you can enable the old behavior of Rhino:

    load("nashorn:mozilla_compat.js");
    

    Another problem I ran into is that certain type-conversions when passing data between java and javascript work differently. For example, the object which arrives when you pass a Javascript array to Java can no longer be cast to List, but it can be cast to a Map. As a workaround you can convert the Javascript array to a Java List in the Javascript code using Java.to(array, Java.type("java.util.List"))

提交回复
热议问题