我无法使用 JDK 6 运行以下代码。 它抛出异常:
线程“main”中的异常 org.mozilla.javascript.EcmaError:ReferenceError:未定义“XML”。
我的环境如下:Ubuntu 11.04、JDK 6、Rhino 1.7R2(也用 Rhino 1.7R3 测试过)
String script = "function abc(x) {var dd = new XML();}";
Context context = Context.enter();
try {
ScriptableObject scope = context.initStandardObjects();
Scriptable that = context.newObject(scope);
Function fct = context.compileFunction(scope, script, "script", 1, null);
Object result = fct.call(context, scope, that, new Object[] {2});
}
catch(Exception e){
e.printStackTrace();
}
finally {
Context.exit();
}
最佳答案
我遇到了同样的问题 → 在 Rhino shell E4X 中工作得很好,但在嵌入式模式下 – ReferenceError: "XML"is not defined.
这是旧 Xerces 的问题,运行我的应用程序时我的类路径中有 2.6.2。当我将它更新到 2.11 时,问题就消失了。
$ java -cp js.jar:xerces-2.6.2.jar org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 3 2011 05 09
js> var x = <foo/>;
js: uncaught JavaScript runtime exception: ReferenceError: "XML" is not defined.
和
$ java -cp js.jar org.mozilla.javascript.tools.shell.Main
Rhino 1.7 release 3 2011 05 09
js> var x = <foo/>;
js> x.toXMLString();
<foo/>
看起来当 Rhino 找到 Xerces 时,它使用它,而不是自己的机制来解析 XML(当我仅在类路径中使用 js.jar 启动 Shell 时)。
关于java - Rhino 1.7r2-3 E4X "XML"未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8182992/