Re: Can't find my own exception class via JNI



On Wed, 30 Nov 2005 04:00:01 GMT, Paul J. Lucas wrote:
> 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

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
.



Relevant Pages

  • Re: JNI FindClass problem
    ... While googling this I found people complaining about FindClass ... an exception being thrown before the function returns. ...
    (comp.lang.java.programmer)
  • JNI FindClass problem
    ... I'm trying to locate a Java class from a C++ method using FindClass ... an exception being thrown before the function returns. ...
    (comp.lang.java.programmer)
  • Cant find my own exception class via JNI
    ... always fails because FindClass() never finds MyExceptionClass, ... FindClass works fine for standard Java exception ... So why can't it find my exception class? ...
    (comp.lang.java.programmer)
  • Re: How to create an instance of a class by class name in a string?
    ... The classes have to descend from tPersistent. ... There is also FindClass, ... unlike GetClass it raises an exception if the class isn't registered. ...
    (comp.lang.pascal.delphi.misc)