我正在尝试在Android的Java应用程序中使用Mozilla Rhino来评估一些JavaScript.我正在使用Eclipse + ADT插件.
首先,我尝试从Mozilla的网站下载Rhino .jar文件,并将其作为Eclipse中的库添加到项目中.Eclipse认识到它很好并编译了应用程序.但是,在运行它时,我在调用时会遇到异常Context.evaluateReader()(请参阅下面的堆栈跟踪).
然后我尝试将Rhino源代码添加为Eclipse中的单独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[]) …Run Code Online (Sandbox Code Playgroud) import android.widget.Toast;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public void bEqual(View v) throws ScriptException {
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
String value = inputText.getText().toString();
Toast.makeText(this,value,Toast.LENGTH_LONG).show();
try{
result = (double)engine.eval(value);
Toast.makeText(this,String.format("%f",result),Toast.LENGTH_SHORT).show();
}catch(Exception e){
Toast.makeText(this,"Exception Raised",Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
这其中有什么问题吗?执行此操作时应用程序正在退出。它没有显示任何错误,但应用程序正在关闭