I have a function in c that adds a row to a table. The function takes as arguments various orderings of ints, floats, and strings by using an ellipsis add_row(int arg1, int arg2, ...) and parses this information based on how the columns are set up.
I need to call this function from Java and am using JNI. I’m not sure what the best way to do this is especially with Java’s stricter typing. I’ve considered a few solutions but none of them seem any good / I’m not sure how to implement them: passing everything as strings, passing a jobjectArray, or passing cell values one at a time.
Any help is much appreciated.
Thanks,
Ben
This is less a problem with Java and JNI and more a problem of how to call a var args function in C with a dynamic list of arguments.
See Calling a C function with a varargs argument dynamically which suggests having two versions of the var args function (although I think the convention is more to allow passthrough of an existing
va_list, rather than for constructing one (which seems to be quite involved)).The JNI bit should be just to define a Java native method with object array arguments which will have a C equivalent receiving the array. Use the JNI API to convert the values into the C equivalents (ints and ANSI strings) then load them into the var args structure and call your
vadd_row()function.Java:
C:
[1] https://bbs.archlinux.org/viewtopic.php?pid=238721
Is it worth the hassle?
Consider just writing a simpler C function that receives the arguments in an array, then create a wrapper that uses var args if needed.