wood burning stoves 2.0*
The moose likes Java in General and the fly likes JNI Invocation Debugging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "JNI Invocation Debugging" Watch "JNI Invocation Debugging" New topic
Author

JNI Invocation Debugging

Markus Moos
Greenhorn

Joined: Jun 06, 2002
Posts: 4
I work on a pretty complex native application which loads a dll. This dll starts the jvm by the jni invocation api and executes some java classes. The classes perform some native callbacks into the dll.
The invocation of the jvm and the of my java programm works fine. The problem is that I can't debug my java classes. As described in some jni tutorials I invoke the jvm from my dll with the debug options. Then I try to connect to the jvm with a java debugger which results in a ConnectException.
To reduce the complexity I wrote a little exe containing the invocation code (see at code listening at the end). Unfortunatly I have the same results. The problem is that if I start the java class with the java.exe and with the same options as in the exe everything is ok. I can connect with my debugger and debug the code.
Has anyone an idea what the problem could be and what's the difference in calling a java programm by the invocation api or by the java.exe.
Thanks for any suggestions
The code of the test exe:
*************************
int main() {
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[5];
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
options[0].optionString = "-Djava.class.path=I:/Vorprojekte/InvokeJVM/java/classes";
options[1].optionString = "-Djava.compiler=NONE"; /* disable JIT */
options[2].optionString = "-Xdebug"; /* print JNI-related messages */
options[3].optionString = "-Xnoagent";
options[4].optionString = "-Xrunjdwp:transport=dt_socket,server=y,suspend=y";

vm_args.version = JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 1 ;
vm_args.ignoreUnrecognized = JNI_FALSE;

/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm,(void**) &env,&vm_args);
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
return(1);
}
//execute
cls = (*env)->FindClass(env, "Prog");
if (cls == 0) {
fprintf(stderr, "Can't find Prog class\n");
return(1);
}

mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String V");
if (mid == 0) {
fprintf(stderr, "Can't find Prog.main\n");
return(1);
}
jstr = (*env)->NewStringUTF(env, " from C!");
if (jstr == 0) {
fprintf(stderr, "Out of memory\n");
return(1);
}
args = (*env)->NewObjectArray(env, 1,
(*env)->FindClass(env, "java/lang/String"), jstr);
if (args == 0) {
fprintf(stderr, "Out of memory\n");
return(1);
}
(*env)->CallStaticVoidMethod(env, cls, mid, args);
(*jvm)->DestroyJavaVM(jvm);

return 0;
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JNI Invocation Debugging
 
Similar Threads
Invoking JVM through C++ code
problem of C++ Call java with JNI
JNI : undefined reference to JNI_CreateJavaVM()
JNI_CreateJavaVM returns error
JNI_CreateJavaVM returns with -1