Hi All,

I'm new to JNI. Trying to access a Java class member from native code and over write the value. My Java code is as below:

package test;
public class MainClass
{
private native void accessObject(TestClass o);
static
{
System.loadLibrary("Mylib");
}
public static void main(String[] args)
{
MainClass o = new MainClass();
TestClass obj = new TestClass();
System.out.println("First : "+obj.var);
o.accessObject(obj);
System.out.println("Second :"+obj.var);
}
}

package test;
public class TestClass
{
public int var = 1234;

public void setInt(int val)
{
var = val;
}
}

I have followed all the necessary steps to generate the .h header file using javah tool. My Native code as below:
JNIEXPORT void JNICALL Java_test_MainClass_accessObject(JNIEnv *env, jobject obj, jobject cObj)
{
jclass classObj;
jmethodID mid;
jint x = 56;
printf("In C\n");
classObj = (*env)->FindClass(env, "test/TestClass");
mid = (*env)->GetMethodID(env, classObj, "setInt", "(I)V");
if(mid == 0)
{
printf("Unable to find the method\n");
return;
}
printf("Calling Java method.....\n");
(*env)->CallVoidMethod(env, classObj, mid, x);
printf("Successfull.. returning from C\n");
}

Output: JVM crashes
In C
Calling Java method.....
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d8ed01c, pid=5156, tid=6096
#
# JRE version: 6.0_23-b05
# Java VM: Java HotSpot(TM) Client VM (19.0-b09 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0xfd01c]
#
# An error report file with more information is saved as:
# <My path>\hs_err_pid5156.log
#
# If you would like to submit a bug report, please visit:
# HotSpot Virtual Machine Error Reporting Page
#

Please let me know where am I going wrong... I have referred to couple of tutorials

Calling Java Methods
Fields and Methods

Thank you all.