// File stacktrace.c
#include "StackTrace.h"
#include <stdio.h>
JNIEXPORT jstring JNICALL
Java_StackTrace_dumpCore(JNIEnv *env, jclass class, jint intarg) {
jclass classid;
jmethodID methodid;
printf("In dumpCore\n");
// Problem code. The Class: java.lang.IntegerX does not exist,
// but the exception is cleared and the program continues.
// Ultimately, failing and dumping core when getting the methodid.
// To fix, comment out the following 2 lines.
// /*
classid = (*env)->FindClass(env, "java/lang/IntegerX");
(*env)->ExceptionClear(env);
// */
// Working code. The Class: java.lang.Integer exists. The lines
// following the FindClass manage printing out of a stack trace,
// clearing an exception, and returning to the java main when
// FindClass fails.
//
// Uncomment the following 6 lines and rebuild to see the program work!
/*
classid = (*env)->FindClass(env, "java/lang/Integer");
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
if(classid == NULL) {
return (*env)->NewStringUTF(env, "JNI FindClass failed!");
}
*/
methodid = (*env)->GetStaticMethodID(env, classid, "toBinaryString",
"(I)Ljava/lang/String;");
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
if(methodid == NULL) {
return (*env)->NewStringUTF(env, "JNI GetStaticMethodID failed!");
}
return (*env)->CallStaticObjectMethod(env, classid, methodid, intarg);
} |