You can simplify your Java class and the required JNI code.
Currently, your native method has some issues:
public int native getTimeFromC(object time);
- Parameter is
Object but should be jtime. - Return value doesn't seem to have a purpose.
- Since the method completely initializes a
jtime object, why not create and return a jtime object?
This class definition has a factory method to create the object and a constructor that moves some the initialization work over from the JNI side.
public class jtime { long millscnds; int secs; public jtime(long millscnds, int secs) { this.millscnds = millscnds; this.secs = secs; } public native static jtime FromC(); }
The factory method can be implemented like this:
JNIEXPORT jobject JNICALL Java_jtime_FromC (JNIEnv * env, jclass clazz) { struct time *mytime = getTime(); jmethodID ctor = (*env)->GetMethodID(env, clazz, "<init>", "(JI)V"); jobject obj = (*env)->NewObject(env, clazz, ctor, mytime->milliscnds, mytime->secs); return obj; }
Tip: The javap tool is like javah but shows the signatures of non-native methods. Using javap -s jtime, you can see the signature of the constructor.