从Nashorn切换到Rhino

从Nashorn切换到Rhino

本文介绍了从Nashorn切换到Rhino(Gradle)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Java 8与Kotlin和项目中的其中一个库一起使用( http://michel-kraemer.github.io/citeproc-java/)拒绝在Java 8捆绑的Nashorn JavaScript解释器上运行.解决方案是改用Rhino JavaScript解释器.

I'm using Java 8 with Kotlin and one of the libraries in my project (http://michel-kraemer.github.io/citeproc-java/) refuses to run on the Nashorn JavaScript interpreter bundled with Java 8. The solution is to use the Rhino JavaScript interpreter instead.

我正在使用Gradle构建我的项目.我已经将Rhino添加到我的依赖项中,但是该库仍然坚持使用Nashorn.

I'm using Gradle to build my project. I've already added Rhino to my dependencies, but the library still insists on using Nashorn.

compile 'org.mozilla:rhino:1.7.7.1'

此外,以下代码会产生NullPointerException:

Furthermore the following code produces a NullPointerException:

val m = ScriptEngineManager()
// specifically look for "rhino" engine
val engine = m.getEngineByName("rhino")

所以看来Rhino不在任何地方的classpath上.如何确保Rhino可用于我的应用程序?

So it seems that Rhino is not on the classpath anywhere. How do I make sure Rhino is available to my application?

推荐答案

对于Rhino引擎,您将需要JSR-233桥(因为JDK中不再使用它).您可以使用以下Maven依赖项:

Along with Rhino engine you will need JSR-233 bridge (as it is no longer in JDK). You can use following maven dependencies:

    <dependency>
        <groupId>org.mozilla</groupId>
        <artifactId>rhino</artifactId>
        <version>1.7.7.1</version>
    </dependency>
    <dependency>
        <groupId>cat.inspiracio</groupId>
        <artifactId>rhino-js-engine</artifactId>
        <version>1.7.7.1</version>
    </dependency>

然后,您只需调用正确的脚本引擎即可:

Then you just call right script engine:

new ScriptEngineManager().getEngineByName("rhino");

这篇关于从Nashorn切换到Rhino(Gradle)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:31