从本机代码 (JNI/NDK) 创建、填充和返回 2D 字符串数组

发布于 2024-11-08 04:33:24 字数 951 浏览 0 评论 0原文

我发现这段特定的代码非常困难(尤其是因为我一周前才开始使用 C)。

我一直在努力寻找正确的语法来在C 中正确创建java 字符串数组(即jstring 对象数组,即表示jstring 对象数组的对象)。我一直在使用以下资源,并从中构建了可编译的代码。我不确定之后发生的错误是由于语法不正确还是由于完全不同的原因。由于代码大部分是孤立的,我假设语法不正确。

(Suns 本机编程文档 & Suns JNI 文档

代码可以编译,但在通过之后“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分開簡單 2024-11-15 04:33:24

要获取行类型的 jclass,您可以对其中一行调用 GetObjectClass()。这是有效的:

Main.java

public class Main {

    static {
        System.loadLibrary("mynative");
    }

    private static native String[][] getStringArrays();

    public static void main(String[] args) {
        for (String[]  array : getStringArrays())
            for (String s : array)
                System.out.println(s);
    }
}

mynative.c

static jobjectArray make_row(JNIEnv *env, jsize count, const char* elements[])
{
    jclass stringClass = (*env)->FindClass(env, "java/lang/String");
    jobjectArray row = (*env)->NewObjectArray(env, count, stringClass, 0);
    jsize i;

    for (i = 0; i < count; ++i) {
        (*env)->SetObjectArrayElement(env, row, i, (*env)->NewStringUTF(env, elements[i]));
    }
    return row;
}

JNIEXPORT jobjectArray JNICALL Java_Main_getStringArrays(JNIEnv *env, jclass klass)
{
    const jsize NumColumns = 4;
    const jsize NumRows = 2;

    const char* beatles[] = { "John", "Paul", "George", "Ringo" };
    jobjectArray jbeatles = make_row(env, NumColumns, beatles);

    const char* turtles[] = { "Leonardo", "Raphael", "Michaelangelo", "Donatello" };
    jobjectArray jturtles = make_row(env, NumColumns, turtles);

    jobjectArray rows = (*env)->NewObjectArray(env, NumRows, (*env)->GetObjectClass(env, jbeatles), 0);

    (*env)->SetObjectArrayElement(env, rows, 0, jbeatles);
    (*env)->SetObjectArrayElement(env, rows, 1, jturtles);
    return rows;
}

构建,为了清楚起见,省略了错误处理。

To get a jclass for the row type, you can call GetObjectClass() on one of the rows. This works:

Main.java

public class Main {

    static {
        System.loadLibrary("mynative");
    }

    private static native String[][] getStringArrays();

    public static void main(String[] args) {
        for (String[]  array : getStringArrays())
            for (String s : array)
                System.out.println(s);
    }
}

mynative.c

static jobjectArray make_row(JNIEnv *env, jsize count, const char* elements[])
{
    jclass stringClass = (*env)->FindClass(env, "java/lang/String");
    jobjectArray row = (*env)->NewObjectArray(env, count, stringClass, 0);
    jsize i;

    for (i = 0; i < count; ++i) {
        (*env)->SetObjectArrayElement(env, row, i, (*env)->NewStringUTF(env, elements[i]));
    }
    return row;
}

JNIEXPORT jobjectArray JNICALL Java_Main_getStringArrays(JNIEnv *env, jclass klass)
{
    const jsize NumColumns = 4;
    const jsize NumRows = 2;

    const char* beatles[] = { "John", "Paul", "George", "Ringo" };
    jobjectArray jbeatles = make_row(env, NumColumns, beatles);

    const char* turtles[] = { "Leonardo", "Raphael", "Michaelangelo", "Donatello" };
    jobjectArray jturtles = make_row(env, NumColumns, turtles);

    jobjectArray rows = (*env)->NewObjectArray(env, NumRows, (*env)->GetObjectClass(env, jbeatles), 0);

    (*env)->SetObjectArrayElement(env, rows, 0, jbeatles);
    (*env)->SetObjectArrayElement(env, rows, 1, jturtles);
    return rows;
}

Building, error handling omitted for clarity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文