Switching from Rhino to Nashorn

前端 未结 7 929
死守一世寂寞
死守一世寂寞 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:03

    To use the importClass method on JDK 8, we need to add the following command:

    load("nashorn:mozilla_compat.js");
    

    However, this change affect the execution on JDK 7 (JDK does not gives support to load method).

    To maintain the compatibility for both SDKs, I solved this problem adding try/catch clause:

    try{
        load("nashorn:mozilla_compat.js");
    }catch(e){
    }
    

提交回复
热议问题