问题描述
我正在尝试在 ant 标签中使用最新版本的 Rhino,但它似乎使用的是与 JVM 捆绑在一起的 Rhino 版本.我尝试调整类路径以指定 Rhino 脚本 jar.目前,我尝试了以下方法:
I'm trying to use a recent version of Rhino in an ant tag, but it seems to be using the version of Rhino that comes bundled with the JVM instead. I've tried adjusting the classpath to specify the Rhino script jar. Currently, I've tried the following:
<project default="hello" name="hello-world" basedir=".">
<target name="hello">
<script language="javascript">
<classpath>
<pathelement location="js.jar"/>
</classpath><![CDATA[
x=<hello><world/></hello>
]]></script>
</target>
</project>
注意脚本中包含的 E4X 语法,它应该只适用于 Mozilla 的 Rhino,而不是与 JVM 捆绑在一起的.
Notice the inclusion of E4X syntax in the script, which should work only work in Rhino from Mozilla, and not the one bundled with the JVM.
js.jar 与 ant 脚本位于同一目录中.我还尝试将其重命名为 rhino.jar,因为我认为我已经看到文档表明这是必要的.
js.jar is in the same directory as the ant script. I've also tried renaming it to rhino.jar, as I think I've seen documentation that suggested that this was necessary.
当我运行它时,它给出了以下错误:
When I run it, it gives the following error:
javax.script.ScriptException: sun.org.mozilla.javascript.EvaluatorException: 语法错误 (#3)
javax.script.ScriptException: sun.org.mozilla.javascript.EvaluatorException: syntax error (#3)
因此,它看起来好像仍在使用 JVM 附带的 Rhino 版本.如何让它使用新版本?
So, it does appear as though it is still using the version of Rhino that comes with the JVM. How can I make it use the new version?
推荐答案
我使用以下 Ant 项目使其工作:
I've got it working using the following Ant project:
<project default="hello" name="helloworld" basedir=".">
<target name="hello">
<script language="javascript" manager="bsf">
<classpath>
<fileset dir="rhino-lib" includes="*.jar"></fileset>
</classpath><![CDATA[
x=<hello><world/></hello>
echo = helloworld.createTask("echo");
for (i=1; i<=10; i++) {
echo.setMessage(i*i);
echo.perform();
}
echo.setMessage(x);
echo.perform();
]]></script>
</target>
</project>
./rhino-lib 中需要以下 jars:
You need the following jars in ./rhino-lib:
不是最漂亮的,但它有效.我会再多尝试一下,看看我是否可以让它与 javax.script 一起工作.
Not the prettiest, but it works. I will try a bit more to see if I can get it to work with javax.script.
这篇关于在 ant 脚本中使用最新的 Rhino的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!