Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Can't find my own exception class via JNI

Thread view: 
Paul J. Lucas - 30 Nov 2005 04:00 GMT
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
Gordon Beaton - 30 Nov 2005 06:43 GMT
> Given:
>
[quoted text clipped - 17 lines]
> 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

Signature

[  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

Paul J. Lucas - 30 Nov 2005 08:18 GMT
> 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
Gordon Beaton - 30 Nov 2005 07:37 GMT
>     It was a mistake in my transcription to Usenet.  The function
>     should have been (and actually is):
[quoted text clipped - 7 lines]
>       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

Signature

[  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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.