I have a native function call that calls my c++ code and this code creates an instance of my user defined class.
This function has the following signature:
public native Object loadEngine(int arg);
After this call has been made, I would like to call more native functions that would take the “Object” returned from the loadEngine() and modify it, or request data from it.
An example of signature:
public native String loadEngine(Object engine, int queryID);
- Is it even possible to do this ?
- If it is possible, how would i convert my GameEngine datatype to java Object or jni jobject.
- Java would automatically pass this as a reference so that it would never be copied and i would be able to modify it, right ?
The following scenario generates an error saying that it can’t convert GameEngine to jobject which is understandable but that’s my best shot having very very little experience with Java:
JNIEXPORT jobject JNICALL Java_package_loadEngine
(JNIEnv *env, jobject obj, jint arg) {
GameEngine engine(params);
return (jobject)engine;
}
JNIEXPORT jstring JNICALL Java_package_queryAction
(JNIEnv *env, jobject obj, jobject engine, jint arg) {
String ret = newString(Integer.toString((GameEngine)engine.unimportant()));
return ret;
}
Since I’m calling native java code via jni to c++, I unfortunatily can’t define the native function as:
public native GameEngine loadEngine(int arg);
Understandable, yet I can’t seem to be able to figure out a workaround.
Thanks, Scarlet.
You could create Java wrapper classes that contain, own, and manage your native code. For instance (off the top of my head, brain-compiled):
And in your JNI implementation:
Note the use of
longto represent a native pointer. This is how the Java runtime does so when needed (e.g. in thejava.nioandjava.util.zippackages.)longrather thanintensures that the type is wide enough to hold a pointer even on 64-bit systems.