如何使用J2V8在Android中使用NodeJS
我使用J2V8库为Android创建了代码,用于在Android移动中执行nodejs脚本。但它给我错误,当我运行应用程序。
Gradle 依赖项
compile 'com.eclipsesource.j2v8:j2v8:4.6.0@aar'
法典
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_console);
runScript();
}
private void runScript() {
NodeJS nodeJS = NodeJS.createNodeJS();
try {
File script = createTempScript("console.log(\"Hello NodeJS\")");
nodeJS.exec(script);
script.delete();
} catch (Exception e) {
e.printStackTrace();
} finally {
nodeJS.release();
}
}
private File createTempScript(String script) throws IOException {
File file = File.createTempFile("temp",".js", getCacheDir());
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(script);
fileWriter.close();
return file;
}
...
错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.asissuthar.lion/in.asissuthar.lion.ConsoleActivity}: java.lang.UnsupportedOperationException:
StartNodeJS Not Supported.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2348)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5345)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
请帮我解决这个错误。
正常的V8引擎工作正常,但上面给出了错误。createNodeJS
V8 v8 = V8.createV8Runtime()