在 Java 和 C 之间传递数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4324819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:52:52  来源:igfitidea点击:

pass data between Java and C

javacjava-native-interface

提问by

I have a C structure.

我有一个C结构。

struct data{
    double value1[50];
    double value2[50];
    int count;
};

I want to map data from java to this C structure.How can I do it using JNI? The java code will not be programmed by me. The java programmer just wants to know in which form should he send me the data? Should he expect any more details

我想将数据从 java 映射到这个 C 结构。我怎样才能使用 JNI 做到这一点?java代码不会由我编写。java程序员只想知道他应该以哪种形式向我发送数据?他应该期待更多细节吗?

I am currently testing my code by filling the structure instance with a CSV file containing 2 columns.

我目前正在通过使用包含 2 列的 CSV 文件填充结构实例来测试我的代码。

I also want to return 3 double values from my C code to the java application.

我还想将 3 个双精度值从我的 C 代码返回到 java 应用程序。

采纳答案by Vladimir Ivanov

Provide the following declaration to your java programmer:

向您的 Java 程序员提供以下声明:

public native double[] doData(double [] value1, double [] value2, int count);

In your c code you will be able to fill the structure with passed parameters. JNI header will look like this:

在您的 c 代码中,您将能够使用传递的参数填充结构。JNI 标头将如下所示:

JNIEXPORT jdoubleArray JNICALL Java_package_className_doData(JNIEnv * , jobject, jdoubleArray , jdoubleArray, jint);

回答by dacwe

If you want to use the same kind of "struct" on the java side I would create a class that corresponds to that struct (so that the java and the c developer can talk about the same thing):

如果您想在 java 端使用相同类型的“结构”,我将创建一个与该结构相对应的类(以便 java 和 c 开发人员可以谈论相同的事情):

class JavaData {
    double value1[50];
    double value2[50];
    int count;
};

Then you need to copy the values from / to the java world when going into JNI. This is an example how you do it:

然后你需要在进入 JNI 时将值从 / 复制到 java 世界。这是你如何做到的一个例子:

// get the class
jclass javaDataClass = env->FindClass("JavaData");

// get the field id
jfieldID countField = env->GetFieldID(javaDataClass, "count", "I");

// get the data from the field
cData.count = env->GetIntField(jData, countField);

... (the rest of the parameters)

回答by aksarben

You can probably get this to work with JNI, but personally, I'd rather have hot needles stuck in my eyes than work with JNI. I personally find Java Native Access (JNA) to be mucheasier to use.

你可能可以让它与 JNI 一起工作,但就我个人而言,我宁愿让热针卡在我的眼睛里,也不愿与 JNI 一起工作。我个人认为 Java Native Access (JNA)易于使用。

回答by Jay

While there is nothing wrong with the CSV format to receive data from different programs, I lean toward using XML to do it. Part of it is subjective preference for the data format. With XML you can define a robust construct that you can validate before you read into your program. And IMHO, a proper XML design is much more readable for debugging purposes.

虽然从不同程序接收数据的 CSV 格式没有任何问题,但我倾向于使用 XML 来做这件事。部分原因是对数据格式的主观偏好。使用 XML,您可以定义一个健壮的构造,您可以在读入程序之前对其进行验证。恕我直言,为了调试目的,适当的 XML 设计更具可读性。

回答by Luca Matteis

Using a standard data container such as CSV is totally appropriate. There's tons of free Java CSV encoding libraries, and the same goes for C.

使用标准数据容器(如 CSV)是完全合适的。有大量免费的 Java CSV 编码库,C 也是如此。

Your Java programmer can still use native Java data structures, all he/she needs to do is encode it into CSV (or other formats such as XML) and send it to you.

您的 Java 程序员仍然可以使用原生 Java 数据结构,他/她所需要做的就是将其编码为 CSV(或其他格式,例如 XML)并将其发送给您。

On your end all you have to do is read this data container using some CSV library.

最后,您所要做的就是使用一些 CSV 库读取这个数据容器。