call nonstatic methods from java to cpp using JNI -
i trying call non static method java c++ using jni java code here:
public class hellojava { public static void main(string args[]) { system.out.println("hello world!"); system.out.println("this main function helloworld java class."); } public void message() { system.out.println("call object"); }
}
and c++ code here:
#include <stdio.h> #include <jni.h> jnienv* create_vm(javavm ** jvm) { jnienv *env; javavminitargs vm_args; javavmoption options; options.optionstring = "-djava.class.path=/home/../nonstaticjavamethods/"; //path java source code vm_args.version = jni_version_1_6; //jdk version. indicates version 1.6 vm_args.noptions = 1; vm_args.options = &options; vm_args.ignoreunrecognized = 0; int ret = jni_createjavavm(jvm, (void**)&env, &vm_args); if(ret < 0) printf("\nunable launch jvm\n"); return env; } int main(int argc, char* argv[]) { jnienv *env; javavm * jvm; env = create_vm(&jvm); if (env == null) return 1; //jclass clsh=null; jmethodid midmain = null; jstring square; jclass clsh = env->findclass("helloworld"); jmethodid constructor = env->getmethodid(clsh, "<init>", "void(v)"); jobject object = env->newobject(clsh, constructor); //obtaining method ids if (clsh != null) { midmain = env->getmethodid(clsh, "message", "void(v)"); env->callvoidmethod(clsh, midmain, object,null); } else { printf("\nunable find requested class\n"); } //release resources. int n = jvm->destroyjavavm(); return 0; } code compiles giving me runtime error. following error
a fatal error has been detected java runtime environment:
sigsegv (0xb) @ pc=0x00007fdf3f5126bb, pid=11302, tid=140596827092800 jre version: openjdk runtime environment (7.0_55-b14) (build 1.7.0_55-b14) java vm: openjdk 64-bit server vm (24.51-b03 mixed mode linux-amd64 compressed oops) problematic frame: v [libjvm.so+0x5c46bb] alloc_object(_jclass*, thread*)+0x1b failed write core dump. core dumps have been disabled. enable core dumping, try "ulimit -c unlimited" before starting java again error report file more information saved /home/../nonstaticjavamethods/hs_err_pid11302.log aborted!
additionally immibis' answer, think call
jclass clsh = env->findclass("helloworld");
isn't returning anything, class called
public class hellojava
so application seg-faulting in getmethodid()
or newobject()
Comments
Post a Comment