打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Problems using Rhino on Android

I'm trying to use Mozilla Rhino in my Java application for Android to evaluate some JavaScript. I am using Eclipse + ADT plugin.

First I tried simply downloading the Rhino .jar file from Mozilla's website and adding it to the project as a library in Eclipse. Eclipse recognised it fine and compiled the application. However, when running it I get an exception when calling Context.evaluateReader() (see below for stack trace).

Then I tried adding the Rhino source code as a separate Android project in Eclipse, marking it as a library and referencing it in my project, which was enough to get Eclipse to get it to compile, but led to the same error.

This is the stacktrace I get (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

The bit of my code that hits the exception looks like this:

        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) {            // ...        }

I also found this blog post which contains similar code and says it works. The author says he used a jar file from the Android Scripting site. The only jar file I found there was in rhino_extras_r3.zip. However, it doesn't contain .class files but rather a classes.dex file. When I added this as a library in Eclipse, it didn't recognise the classes it contains and thus failed to compile my project because of the missing references to Rhino classes.

Any help at all on how to get this to work is appreciated!

asked Oct 4 '10 at 21:33
Liquid_Fire
2,86611320

2 Answers

up vote 28 down vote accepted

I finally got it to work. I should have paid more close attention to that blog post I linked.

If I add the line

cx.setOptimizationLevel(-1);

to disable optimisations, everything works perfectly.

answered Oct 4 '10 at 22:02
Liquid_Fire
2,86611320

12  
By default, Rhino tries to generate JVM bytecode on the fly, as a sort of Javascript JIT. That's cool and all, but Android does not run JVM bytecode -- it runs Dalvik bytecode. Hence, you have to disable optimization, via the code snippet you showed, to allow Rhino to run. –  CommonsWare Oct 4 '10 at 22:06
    
I confirm this helps :-) –  loki2302 Jul 24 '12 at 10:33
1  
@Listill m face same problem after add this line.. –  Parag Chauhan Oct 27 '12 at 12:39
    
You can use V8 instead of rhino –  Crossle Song May 20 '13 at 4:15
    
Awesome, helped me a lot :) –  Ragunath Jawahar Jun 20 '13 at 18:03

An Android-compatible edition of rhino1_7R2.jar is available on the SL4A site, in the version control system. Here is a sample project that wraps up Rhino and BeanShell into an Android interpreter service.

answered Oct 4 '10 at 21:42
CommonsWare
480k5010781136

    
Thanks, I didn't notice that jar file because it wasn't listed in the downloads section. It still didn't solve my problem (I've posted my actual solution), but you definitely deserve an upvote for showing me where the jar file is and the sample project that I can use as an example. –  Liquid_Fire Oct 4 '10 at 22:06
1  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
The method find(String, Object[]) is ambiguous for the type
Java 8新特性探究(十二)Nashorn :新犀牛
Android中Context简介
基于mozilla的js.jar包定义javascript的解析ScriptTool脚本工具类实现执...
Android apk的安装、卸载、更新升级(通过Eclipse实现静默安装)
Android mqtt即时通讯方案
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服