从本机代码 (JNI/NDK) 创建、填充和返回 2D 字符串数组
我发现这段特定的代码非常困难(尤其是因为我一周前才开始使用 C)。
我一直在努力寻找正确的语法来在C 中正确创建java 字符串数组(即jstring 对象数组,即表示jstring 对象数组的对象)。我一直在使用以下资源,并从中构建了可编译的代码。我不确定之后发生的错误是由于语法不正确还是由于完全不同的原因。由于代码大部分是孤立的,我假设语法不正确。
代码可以编译,但在通过之后“FindClass”代码行发送了一个 SIGSEGV 信号,该信号杀死了 C 进程:
jint size = 5;
jclass StringObject = (*env)->FindClass(env, "java/lang/String");
jobjectArray sampleMessage = (*env)->NewObjectArray(env, size, StringObject, NULL);
jobjectArray returnArray = (jobjectArray) (*env)->NewObjectArray(env, messageCount, &sampleMessage, 0);
有人能给我指出一个有用的资源吗?或者确认语法是否正确。
编辑
我的问题很大一部分是调试这段代码导致了问题。我没有时间缩小重现因素,但通过 eclipse 在 gdb 客户端中单步执行 JNI 代码不起作用。
I'm finding this particular bit of code quite difficult (Not least of which because I only started playing with C a week ago).
I've been trying hard to find the right syntax to correctly create a java string array in C (i.e., an array of jstring objects, i.e. an object which represents an array of jstring objects). I've been using the following resources and from them I've constructed code which compiles. I'm not sure if the error which occurs afterwards is due to the syntax being incorrect or because of a completely separate reason. Since the code is mostly in isolation I'm assuming the syntax is incorrect.
(Suns Native Programming Documentation & Suns JNI documentation)
The code compiles but after passing the "FindClass" line of code a SIGSEGV signal is sent which kills the C process:
jint size = 5;
jclass StringObject = (*env)->FindClass(env, "java/lang/String");
jobjectArray sampleMessage = (*env)->NewObjectArray(env, size, StringObject, NULL);
jobjectArray returnArray = (jobjectArray) (*env)->NewObjectArray(env, messageCount, &sampleMessage, 0);
Could anyone point me to a useful resource for this? Or confirm the syntax is correct.
EDIT
I large part of my problem was that debugging this code caused the problem. I don't have time to narrow down the reproducing factor but stepping over JNI code in a gdb-client through eclipse DOESN'T work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获取行类型的 jclass,您可以对其中一行调用
GetObjectClass()
。这是有效的:Main.java
mynative.c
构建,为了清楚起见,省略了错误处理。
To get a jclass for the row type, you can call
GetObjectClass()
on one of the rows. This works:Main.java
mynative.c
Building, error handling omitted for clarity.