JNI FindClass problem

P

Praetorian

Hi,
I'm trying to locate a Java class from a C++ method using FindClass
but it always fails. FindClass succeeds if I change the argument from
my class name to "java/lang/String" so it seems the class loader
cannot locate my class. Here's my setup:

I have 3 class files:
C:\Work\jni\test\src\CANTest.java
C:\Work\jni\test\src\com\CompanyName\util\CANUSB.java
C:\Work\jni\test\src\com\CompanyName\util\CANMessage.java

CANTest.java only contains a main() function that launches a JPanel,
initializes the CANUSB class and uses it. The CANUSB class loads a DLL
to access functions from another 3rd party DLL. The .class files are
in the following directories:

C:\Work\jni\test\bin\CANTest.class
C:\Work\jni\test\bin\com\CompanyName\util\CANUSB.class
C:\Work\jni\test\bin\com\CompanyName\util\CANMessage.class

I'm calling the CANTest class using the following command

java -classpath C:\Work\jni\test\bin CANTest


The program works fine until the native method that invokes FindClass
is called. This call is as follows:

jclass canMsgCls = jEnv->FindClass( "com/CompanyName/util/
CANMessage" );

While googling this I found people complaining about FindClass
returning NULL but that doesn't happen in my case. The call results in
an exception being thrown before the function returns. The exception
is always thrown in
SetByteArrayRegion which FindClass seems to be calling.

What am I doing wrong here?

Thanks in advance for your help,
Ashish.
 
L

Lew

Praetorian said:
The program works fine until the native method that invokes FindClass
is called. This call is as follows:

jclass canMsgCls = jEnv->FindClass(
"com/CompanyName/util/CANMessage" );

I'm not familiar with JNI, but in Java you'd need
"/com/CompanyName/util/CANMessage" (with the leading slash). Could that be
the same in JNI calls?
 
P

Praetorian

I'm not familiar with JNI, but in Java you'd need
"/com/CompanyName/util/CANMessage" (with the leading slash).  Could that be
the same in JNI calls?

Thanks for the reply Lew, I already tried using the leading slash and
that didn't help. But I just figured out my problem. One thing I
failed to mention in my post (because I didn't think it was important)
was that the FindClass call is being made from within a callback
function. So I was caching the JNIEnv * from a previous call to use
with FindClass. This link (http://java.sun.com/docs/books/jni/html/
other.html#26206) explains that such caching is illegal and will cause
problems.
 
R

Roedy Green

While googling this I found people complaining about FindClass
returning NULL but that doesn't happen in my case. The call results in
an exception being thrown before the function returns. The exception
is always thrown in
SetByteArrayRegion which FindClass seems to be calling.

Trap that exception with a catch (Exception e) with a stack trace etc
to get some clues.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Out of 135 criminals, including robbers and rapists, 118 admitted that when they were children they burned, hanged and stabbed domestic animals."
~ Ogonyok Magazine 1979.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top