Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with AttachCurrentThread

843829Oct 2 2006 — edited Oct 3 2006
Does anyone know what value is returned when AttchCurrentThread is succesful? I'm trying to call into a java function from the native callback code but have been unsuccesful. The code looks as follows:

////////////////////
static JavaVM *jvm = NULL;

void Callback cb(){
JNIEnv* jenv;
int res = jvm->AttachCurrentThread((void **)&jenv, NULL);

jclass jcls = jenv->FindClass("classname");
jmethodID methID =
jenv->GetStaticMethodIDjcls, "javaclass", "()V");
jenv->CallStaticVoidMethod(jcls, methID);
}

JNIEXPORT jint JNICALL Java_com_fn1(JNIEnv *env, jclass cls){
if (jvm == NULL) env->GetJavaVM(&jvm);
if(..)
//call call bck function
cb();

}
/////////////////////////////////////////////////////

AttachCurrentThread returns zero and the classname, methodname derived from the jenv are all null ( jcls, methID etc). Anything wrong with the above code?

For testing, I put the same code in native method as below, AttachCurrentThread still returns 0 but it works fine and calls the java method. Any ideas on what's going wrong with the callback code? Thanks

JNIEXPORT jint JNICALL Java_com_fn1(JNIEnv *env, jclass cls){
if (jvm == NULL) env->GetJavaVM(&jvm);

JNIEnv* jenv;
int res = jvm->AttachCurrentThread((void **)&jenv, NULL);
jclass jcls = jenv->FindClass("classname");
jmethodID methID =
jenv->GetStaticMethodIDjcls, "javaclass", "()V");
jenv->CallStaticVoidMethod(jcls, methID);

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 31 2006
Added on Oct 2 2006
1 comment
141 views