JNI access violation when calling non static java method from C++ -


i'm trying call non static java method c++.

sample2.java:

public class sample2 {     public int intmethod(int n) {         return n*n;     } } 

jnitest.cpp:

#include "stdafx.h" #include <iostream> #include <string> #include <memory.h> #include <jni_md.h> #include <jni.h> using namespace std;  #ifdef _win32 #define path_separator ';' #else #define path_separator ':' #endif  int _tmain(int argc, _tchar* argv[]) {     javavmoption options[3];     static jnienv *env;     javavm *jvm;     javavminitargs vm_args;     long status;     jclass cls, stringclass;     jmethodid mid;     jstring jstr;     jobjectarray args;      jint square;      options[0].optionstring = "-djava.class.path=d:\\studie\\exp\\code\\workspace\\jnitest\\bin"; //2apl\\build"; //workspace\\jnitest\\bin";     options[1].optionstring = "-verbose";     options[2].optionstring = "-verbose:jni";     memset(&vm_args, 0, sizeof(vm_args));     vm_args.version = jni_version_1_6;     vm_args.noptions = 1;     vm_args.options = options;     status = jni_createjavavm(&jvm, (void**)&env, &vm_args);      if (status != jni_err)     {         cls = env->findclass("sample2");         if(cls !=0) {             mid = env->getmethodid(cls, "intmethod",  "(i)i");             if(mid !=0) {                 square = env->callintmethod(cls, mid, 5); //this crashes                 printf("result of intmethod: %d\n", square);             }         }         jvm->destroyjavavm();         return 0;     }     else {         return -1;     } } 

the program finds method , gets square = env->callintmethod(cls, mid, 5); part access violation occurs. if change static method runs fine, need able call non static methods well...

any thoughts on i'm doing wrong here?

after you've called findclass, need create instance of class calling newobject. so, first need constructor ...

jmethodid constructor = (*env)->getmethodid(env, cls, "<init>", "void(v)"); 

then create object

jobject object = (*env)->newobject(env, cls, constructor); 

then can call instance function

square = env->callintmethod(cls, mid, object, 5);  

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -