Objective C calling Java methods using JNI(Objective C 使用 JNI 调用 Java 方法)
问题描述
Can someone show me how to call a Java method from Objective C.
In more detail this is actually what I would like to do
1) Have first call from the java side to the objective C. during this call I would like to get a reference to the java object.
2) Later down the line I would like Objective C to use the reference got in the previous step to call Java methods.
Thanks
The following seems to work. It is based on the above comment on following the C examples and this link.
http://urlgrey.net/?p=121
As the link says do not create a global variable for the env instead create a global variable of the jvm, also create a global reference to your class.
Here is how I implement the step 1 : " 1) Have first call from the java side to the objective C. during this call I would like to get a reference to the java object. "
First declare global variable in the header file for
1) the jvm:
2) The Java class:
3) The java Object:
Next in the call that comes from the Java side to the Objective C set the values for these variables
1) For the JVM:
2) For the Object:
3) For the Class:
Here is the link from where I got the code for the class
http://java.sun.com/docs/books/jni/html/refs.html
Now the second step
" 2) Later down the line I would like Objective C to use the reference got in the previous step to call Java methods "
To call from Objective C back to Java you need to make sure that the call is done on the same thread by which Java called Objective C, so here is the code.
Hope this helps.
这篇关于Objective C 使用 JNI 调用 Java 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!