Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

pass data between Java and C

I have a C structure.

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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):

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:

// 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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...