j2v8 android,java - How to use NodeJS in Android using J2V8 - Stack Overflow

这篇博客讨论了作者在尝试使用J2V8库在Android应用中运行Node.js脚本时遇到的一个错误。具体问题是调用`NodeJS.createNodeJS()`时抛出了`UnsupportedOperationException: StartNodeJSNotSupported`。尽管标准的V8引擎工作正常,但集成NodeJS支持出现了问题。作者提供了相关代码片段,并寻求解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have created code for android using J2V8 library for executing nodejs script in android mobile. but it gives me error when i run application.

Gradle dependencies

compile 'com.eclipsesource.j2v8:j2v8:4.6.0@aar'

Code

...

@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;

}

...

Error

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)

Please Help me to solve this error.

Normal V8 engine works fine but above createNodeJS gives error.

V8 v8 = V8.createV8Runtime()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值