Passing data types from Java to C (or vice versa) using JNI?

If you're going to be doing this with a lot of objects, something like Swig would be best. You could use jobject type to pass around custom objects. The syntax isn't nice, perhaps there is a better way to write this.

Up vote 1 down vote favorite 1 share g+ share fb share tw.

I see a mapping of primitive datatypes to types in C however not too sure if we can send across our own data types (e.g. Send across or return an Employee object or something! ). Java c jni link|improve this question asked Mar 23 '10 at 23:18user277460612.

If you're going to be doing this with a lot of objects, something like Swig would be best. You could use jobject type to pass around custom objects. The syntax isn't nice, perhaps there is a better way to write this.

Example Employee object: public class Employee { private int age; public Employee(int age) { this. Age = age; } public int getAge() { return age; } } Call this code from some client: public class Client { public Client() { Employee emp = new Employee(32); System.out. Println("Pass employee to C and get age back: "+getAgeC(emp)); Employee emp2 = createWithAge(23); System.out.

Println("Get employee object from C: "+emp2.getAge()); } public native int getAgeC(Employee emp); public native Employee createWithAge(int age); } You could have JNI functions like this for passing an employee object from Java to C, as a jobject method argument: JNIEXPORT jint JNICALL Java_Client_getAgeC(JNIEnv *env, jobject callingObject, jobject employeeObject) { jclass employeeClass = (*env)->GetObjectClass(env, employeeObject); jmethodID midGetName = (*env)->GetMethodID(env, employeeClass, "getAge", "()I"); int age = (*env)->CallIntMethod(env, employeeObject, midGetName); return age; } Passing an employee object back from C to Java as a jobject, you could use: JNIEXPORT jobject JNICALL Java_Client_createWithAge(JNIEnv *env, jobject callingObject, jint age) { jclass employeeClass = (*env)->FindClass(env,"LEmployee;"); jmethodID midConstructor = (*env)->GetMethodID(env, employeeClass, "", "(I)V"); jobject employeeObject = (*env)->NewObject(env, employeeClass, midConstructor, age); return employeeObject; }.

You can. If you want to make Java/C translation easier, check out Swig, which allows you to translate between Java and C/C++ structures in a straightforward fashion..

Stackoverflow.com/questions/6215374/from-c-code-to-java-and-jni – ViTo Brothers Apoyan Jun 3 '11 at 11:31.

You can use JNA to easily pass structures around. JNA allows you to write your binding completely in Java code. :-).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions