Click here to Skip to main content
15,885,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a int array like

int[][] coordinates = {{346, 640, 499, 666},{555, 640, 675, 666}};


and I am trying to send this array to C with using JNI. I could find clear solution for one dimensional array but not for multidimensional array.
<br />
public class PopulateCoordinates{<br />
public native void Coordinates(int[][] coordinates);<br />
<br />
 static {<br />
  System.loadLibrary("libPopulateCoordinates");<br />
 }<br />
 public static void main(String[] args) {<br />
<br />
  PopulateCoordinates h = new PopulateCoordinates();<br />
  int[][] coordinates = {{346, 640, 499, 666},{555, 640, 675, 666}};<br />
  h.Coordinates(coordinates);<br />
 }<br />
 }<br />

My C code has method which will populate its array that is passed from java like this

rect skip_rectarr[2] = { {346, 640, 499, 666}, { 555, 640, 675, 666 }};

<br />
typedef struct {<br />
int left;<br />
int top;<br />
int right;<br />
int bot;<br />
} rect;<br />
JNIEXPORT void JNICALL PopulateCoordinates_Coordinates(JNIEnv *env, jobject obj,jobjectArray inJNIArray)<br />
{<br />
//rect skip_rectarr[2] = { {346, 640, 499, 666}, { 555, 640, 675, 666 }};<br />
}<br />


I am having a hard time in properly converting jobject to int[][] due to lack of native development knowledge. Can anyone tell me the simplest and safest possible way? Thanks
Posted
Updated 21-Mar-15 9:49am
v2
Comments
Mohibur Rashid 21-Mar-15 22:56pm    
You can do other approach to send data. Such as client-server model.

1 solution

To be honest: The simplest and savest way is to use one dimension arrays with native data types and AVOID structs. Java and C have different memory layouts. Microsoft has solved this mess on its .net-languages.

I would extend the interfaces of rhe func with size information:

C++
JNIEXPORT void JNICALL PopulateCoordinates_CoordinatesRect(JNIEnv *env, jobject obj,jobjectArray inJNIArray, int countObjectes)


You shouldnt fizzle to much on the interfaces, but better transfer simple and native data. Or you may waiste a lot of time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900