HELP!!! URGENT!!!How do I make JNIEnv and jobject global

Hi!

My JNI code uses a library made by another company. The mentioned library accepts requests but does not return the result/answer of requests in the usual way as return result. Instead, it calls my JNI callback method. The problem here is that my JNI callBack method in turn needs to call my java callback. Since the JNI callback function does not have the JNIEnv and jobject sent by java, it can not call the method callback in java since for it to do that, it has to have JNIEnv and jobject. I tried to declare global JNIEnv and global jobject in this manner:

JNIEnv *envGlobal;

static jobject savedReference;

In the native methods where I accepted JNIEnv as arguments, I assigned the the envGlobal this way:

envGlobal = (JNIEnv*) malloc(sizeof(JNIEnv));

envGlobal = env; // where env is an argument of type JNIEnv of some native method

I also assigned the savedReference variable as

savedReference = env->NewGlobalRef(thisObject); // I got this declaration from the Essential JNI book by Rob Gordon

but it resulted to : "request for member 'NewGlobalRef' to something not a structure or a union"

I really need to know how to do this. I need the JNIEnv and jobject references to connect back to my callBack method in java. Please, please, please help me. I will really appreciate any help. Thanks :-)

Monette

[1412 byte] By [mon03] at [2007-9-26 8:16:01]
# 1

If I undertand your process flow correctly, it is

java program -> your dll

your dll -> other library

other library -> call back routine.

And eventually, this all unwinds.

So what you want is for the callback routine to be able to access the java environment, and the java object which has the native method.

If I have this right, then all you should have to do is the following:

1. In a global space - that is, outside any of the routines declare

JNIEnv * envGlobal;

jobject savedReference;

2. In your native method do the following:

envGlobal = env// The env passed in.

savedReference = obj// The object reference passed in.

3. Go ahead and use these in your callback.

You don't have to malloc space for the environment, and you don't need a new global reference.

Limitation:

If, in the overall processing scheme, the foreign library spawns a thread and lets your native method return immediately, then what I have just shown you won't work, and franly I don't know what WILL work. The JNI is real touchy.

bschauwe at 2007-7-1 18:45:48 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Hi,

the solution is described in "The Java Native Interface" by Sheng Liang (Addison Wesley).

In short:

During loading a JNI lib the JNI_Onload function is called.

The JVM attached to the library is passed to JNI_Onload

and can be cached in a static variable of the lib.

Everytime you need the JNIEnv in a callback, you need

to inquire it with (*cached_jvm)->GetEnv( ... ) since

it might change.

Have fun,

Klaus

wiedkla at 2007-7-1 18:45:48 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
Hi!Thank you both for your responses. I'll try the solutions you offered and will post it here after it works. Thank you so much for your answers. :-)Monette
mon03 at 2007-7-1 18:45:48 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
Hi friends,I need to access Java methods from VC++. Is it possible? Please help me..... ThanksFrancis
francis_ at 2007-7-1 18:45:48 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

Arise, zombie thread! ARISE!

[url=http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html]Advanced Programming for the Java 2 Platform - JNI Technology[/url]

[url=http://www.javaworld.com/javaworld/jw-10-1999/jw-10-jni.html]Enhance your Java application with Java Native Interface[/url]

yawmark at 2007-7-1 18:45:48 > top of Java-index,Java HotSpot Virtual Machine,Specifications...