Return a Structure Object from C to Java Through JNI
Ajay Singh
Ranch Hand
Joined: Jan 04, 2008
Posts: 101
posted
HI all
i am using the JNI to return an integer array object from a C class function to the java code.Now i want to return a structure object containing the student's record (name,age etc) to the java code.can anyone tell me what changes to be made to the following c file
public class Hello { static { System.loadLibrary("hello"); }
public native Object sayHello(int [] arr); public static void main(String[] args) { Hello p = new Hello(); int arr[] = new int[10]; for (int i = 0; i < 10; i++) { arr[i] = i; } int [] len = (int[]) p.sayHello(arr);
System.out.println("sum = " + len.length); //length of the array } }
Any help would be appriciated
Rob Prime
Bartender
Joined: Oct 27, 2005
Posts: 11146
posted
First of all, you will need to create a class similar to the structure in Java - with a String and int field.
Now you have two options: 1) create the object in Java and pass it to the native method. This then sets the fields. 2) create the object in the native method.
Here's a little code to show how an object can be created using JNI: <blockquote>code:<pre name="code" class="core">jclass cls; jmethodID constructor; jvalue args[2]; jobject object;
// get a reference to your class if you don't have it already cls = (*env)->FindClass(env, "mypackage/Student"); // get a reference to the constructor; the name is <init> constructor = (*env)->GetMethodID(env, cls, "<init>", "(ILjava/lang/String;)V"); // set up the arguments; i means int, l means object args[0].i = 23; args[1].l = (*env)->NewStringUTF(env, "Jack"); object = (*env)->NewObjectA(env, cls, constructor, args);</pre></blockquote> <blockquote>code:<pre name="code" class="core">package mypackage;
public class Student { private int age; private String name;
My JVM Crashes getting the following error messages
"An Unexpected Error has been detected by Hotspot Virtual Machine............"
I dont know where i am doing wrong.please help
Rob Prime
Bartender
Joined: Oct 27, 2005
Posts: 11146
posted
The full classname is not Student - it is Hello$Student (since it's an inner class).
Because of this, FindClass will not find the class and return NULL. It also throws a ClassNotFoundException, but the problem with native code is - it doesn't return immediately after throwing an exception but only after the current function has finished. Your error is thrown earlier because of a native "NullPointerException".
Now I've changed the class name, and it can't find the constructor now. If I make the class static it works.
Now I've also tried to make the class non-static and the creation method as well - because you can't create an instance of a non-static inner class inside a static context of course. This didn't solve the issue though.
Therefore, I am sorry to say that I do not know how to create an instance of a non-static inner class in JNI.
subject: Return a Structure Object from C to Java Through JNI