【发布时间】:2011-04-21 00:47:43
【问题描述】:
我正在尝试在我的 Android 的 Java 应用程序中使用 Mozilla Rhino 来评估一些 JavaScript。我正在使用 Eclipse + ADT 插件。
首先,我尝试从 Mozilla 的网站下载 Rhino .jar 文件并将其作为 Eclipse 中的库添加到项目中。 Eclipse 很好地识别它并编译了应用程序。但是,在运行它时,我在调用 Context.evaluateReader() 时遇到异常(请参阅下面的堆栈跟踪)。
然后我尝试在 Eclipse 中将 Rhino 源代码添加为单独的 Android 项目,将其标记为库并在我的项目中引用它,这足以让 Eclipse 编译它,但导致了同样的错误。
这是我得到的堆栈跟踪 (java.lang.UnsupportedOperationException: can't load this type of class file)
Thread [<7> Thread-8] (Suspended (exception UnsupportedOperationException))
DefiningClassLoader(ClassLoader).defineClass(String, byte[], int, int, ProtectionDomain) line: 338
DefiningClassLoader.defineClass(String, byte[]) line: 62
Codegen.defineClass(Object, Object) line: 159
Codegen.createScriptObject(Object, Object) line: 114
Context.compileImpl(Scriptable, Reader, String, String, int, Object, boolean, Evaluator, ErrorReporter) line: 2440
Context.compileReader(Reader, String, int, Object) line: 1326
Context.compileReader(Scriptable, Reader, String, int, Object) line: 1298
Context.evaluateReader(Scriptable, Reader, String, int, Object) line: 1137
TimetableProcessor.evaluate(InputStream, String, String[]) line: 31
TimetableProcessor.processBasicData(InputStream, String) line: 58
TimetableProcessor.process(InputStream, String) line: 52
TimetableUpdater.update() line: 53
Main$1$1.run() line: 22
我的代码中遇到异常的部分如下所示:
Context cx = Context.enter();
cx.setLanguageVersion(Context.VERSION_1_7);
Scriptable scope = cx.initStandardObjects();
try {
Object result = cx.evaluateReader(scope, new InputStreamReader(data), /* <<< exception here */
filename, 0, null);
} catch (IOException e) {
// ...
}
我还发现 this blog post 包含类似的代码并说它可以工作。作者说他使用了来自Android Scripting 站点的jar 文件。我在那里找到的唯一 jar 文件在 rhino_extras_r3.zip 中。但是,它不包含.class 文件,而是包含classes.dex 文件。当我将它作为库添加到 Eclipse 中时,它无法识别它包含的类,因此由于缺少对 Rhino 类的引用而无法编译我的项目。
感谢任何有关如何使其工作的帮助!
【问题讨论】: