jni trouble

S

stef

Hi guys,
Could U tell me where is my error with my little JNI calling :

with VC 6 and JAVA 1.5.2
(i omitted the package/import declaration to gain time)

//--------------------------------------- C method : (generating a test.dll)

JNIEXPORT void JNICALL Java_com_pack_JTest_Connect(JNIEnv *env, jobject
this, jstring Str)
{
jfieldID fid;
jclass cls;
jlong val;

cls = (*env)->GetObjectClass(env, this);
fid = (*env)->GetFieldID(env, cls, "m_hdl", "J");

val = (*env)->GetLongField(env, this, fid);
(*env)->SetLongField(env, this, fid, 666);
}


//--------------------------------------- Java
public class JTest
{
private long m_hdl;

static
{
try
{
System.loadLibrary("test");
}
catch (java.lang.UnsatisfiedLinkError e)
{
System.out.println("error : " + e);
}
}


public static native void Connect( String xmlConnect );


/*
*
*/
public JTest()
{
Connect("");
System.out.println("Test : "+m_hdl);
}
}


Simple no ?
and with a little test like


class main
{
public static void main(String[] args)
{
JTest p;
int i=0;

p = new JTest();
p.Connect("");
}
}


I get :
Exception in thread "main" java.lang.NoSuchFieldError: m_hdl
at com.pack.JTest.Connect(Native Method)
at com.pack.JTest.<init>(JTest.java:35)
at main.main(main.java:18)

As I can see in debug mode, method GetFieldID() doesn't "see" m_hdl !!!!
why ???



thanx for your help
 
G

Gordon Beaton

JNIEXPORT void JNICALL Java_com_pack_JTest_Connect(JNIEnv *env, jobject
this, jstring Str)
{
jfieldID fid;
jclass cls;
jlong val;

cls = (*env)->GetObjectClass(env, this);
fid = (*env)->GetFieldID(env, cls, "m_hdl", "J");

val = (*env)->GetLongField(env, this, fid);
(*env)->SetLongField(env, this, fid, 666);
}
public static native void Connect( String xmlConnect );
I get :
Exception in thread "main" java.lang.NoSuchFieldError: m_hdl
at com.pack.JTest.Connect(Native Method)
at com.pack.JTest.<init>(JTest.java:35)
at main.main(main.java:18)

As I can see in debug mode, method GetFieldID() doesn't "see" m_hdl !!!!
why ???

You have declared the native method using "static", so the second
argument to the native method isn't "this" as you seem to expect, it's
an object representing the JTest *class*.

You then do GetObjectClass() which returns an object representing
java.lang.Class. It doesn't contain the field you're looking for.

The solution is to remove the static declaration from the native
method.

/gordon
 

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,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top