Can't find my own exception class via JNI

P

Paul J. Lucas

Given:

jclass findClassOrDie( JNIEnv *env, char const *name ) {
jclass const theClass = env->FindClass( name );
cerr << "FindClass(" << name << ") failed" << endl;
exit( 1 );
}

void throwMyException( JNIEnv *env, char const *msg ) {
static char const MyExceptionClass[] =
"com/mycompany/utils/MyExceptionClass";
env->ThrowNew( findClassOrDie( env, MyExceptionClass ), msg );
}

Any call to throwMyException() and subsequently to findClassOrDie()
always fails because FindClass() never finds MyExceptionClass, but I
have no idea why not. FindClass works fine for standard Java exception
classes like IllegalArgumentException.

So why can't it find my exception class? It's just a simple class
derived from Exception.

- Paul
 
G

Gordon Beaton

Given:

jclass findClassOrDie( JNIEnv *env, char const *name ) {
jclass const theClass = env->FindClass( name );
cerr << "FindClass(" << name << ") failed" << endl;
exit( 1 );
}

void throwMyException( JNIEnv *env, char const *msg ) {
static char const MyExceptionClass[] =
"com/mycompany/utils/MyExceptionClass";
env->ThrowNew( findClassOrDie( env, MyExceptionClass ), msg );
}

Any call to throwMyException() and subsequently to findClassOrDie()
always fails because FindClass() never finds MyExceptionClass, but I
have no idea why not. FindClass works fine for standard Java exception
classes like IllegalArgumentException.

So why can't it find my exception class? It's just a simple class
derived from Exception.

Where is the class, and what does your classpath contain?

Correct me if I'm wrong, but your findClassOrDie() doesn't appear to
actually return the jclass it is declared to return. Also, it seems to
print the error message and exit unconditionally, without actually
checking whether FindClass() was successful or not. I'd suggest using
ExceptionDescribe() to see the text explaining the reason for failure,
but only when FindClass() fails (i.e. returns NULL or
ExceptionOccurred() is true).

/gordon
 
G

Gordon Beaton

It was a mistake in my transcription to Usenet. The function
should have been (and actually is):

jclass findClassOrDie( JNIEnv *env, char const *name ) {
jclass const theClass = env->FindClass( name );
if ( !theClass ) {
cerr << "FindClass(" << name << ") failed" << endl;
exit( 1 );
}
return theClass;
}

In that case, and assuming that "name" contains the correct name and
spelling of your exception class (in the form you posted at the start
of this thread), then I would start by seeing what ExceptionDescribe()
says when FindClass() fails.

Also try using some JVM arguments like -verbose:class and -verbose:jni
to see if that gives you any more clues.

Finally, re-check the classpath and location of the class...

/gordon
 
P

Paul J. Lucas

Gordon Beaton said:
Where is the class, and what does your classpath contain?

It's in the same place (package and jar) as the class that
contains the native methods.

Correct me if I'm wrong, but your findClassOrDie() doesn't appear to
actually return the jclass it is declared to return.

It was a mistake in my transcription to Usenet. The function
should have been (and actually is):

jclass findClassOrDie( JNIEnv *env, char const *name ) {
jclass const theClass = env->FindClass( name );
if ( !theClass ) {
cerr << "FindClass(" << name << ") failed" << endl;
exit( 1 );
}
return theClass;
}

- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top