java - Error occurred during initialization of VM Unable to load native library: Can't find dependent libraries [Compiling C file, JNI] -
i implementing "reverse" jni call java c following tutorial: how call java functions c using jni. whenever compile file, "ctest.cpp", , run "ctest.exe" in windows command prompt or visual studio, receive following error:
error occurred during initialization of vm unable load native library: can't find dependent libraries
i know there similar questions out there: i've seen them , applied recommendations (such linking jvm.dll , jvm.lib paths path environment variable). added jvm.dll location inside file properties via visual studio. need help- have been stuck on few days now. thank time!
ctest.cpp below:
#include "stdafx.h" #include <stdio.h> #include <jni.h> #include <string.h> #define path_separator ';' /* define ':' on solaris */ #define user_classpath "." /* prog.class */ struct controldetail { int id; char name[100]; char ip[100]; int port; }; struct workorder { char sumserialid[20]; char accessnumber[18]; char actiontype[4]; char effectivedate[24]; char fetchflag[2]; char reason[456]; char accesssource[100]; }; jnienv* create_vm(javavm ** jvm) { jnienv *env; javavminitargs vm_args; javavmoption options; options.optionstring = "-djava.class.path=c:\\users\\my name\\downloads\\src_cjnijava\\java src\\teststruct"; //path java source code vm_args.version = jni_version_1_8; //jdk version. indicates version 1.8 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; struct controldetail ctrldetail; ctrldetail.id = 11; strcpy(ctrldetail.name, "hr-hw"); strcpy(ctrldetail.ip, "10.32.164.133"); ctrldetail.port = 9099; printf("struct created in c has values:\nid:%d\nname:%s\n ip:%s\nport:%d\n", ctrldetail.id, ctrldetail.name, ctrldetail.ip, ctrldetail.port); /********************************************************/ struct workorder wo[2]; strcpy(wo[0].sumserialid, "2000"); strcpy(wo[0].accessnumber, "2878430"); strcpy(wo[0].actiontype, "04"); strcpy(wo[0].effectivedate, "25-12-2007 12:20:30 pm"); strcpy(wo[0].fetchflag, "0"); strcpy(wo[0].reason, "executed successfully"); strcpy(wo[0].accesssource, "pms"); strcpy(wo[1].sumserialid, "1000"); strcpy(wo[1].accessnumber, "2878000"); strcpy(wo[1].actiontype, "t4"); strcpy(wo[1].effectivedate, "25-12-2007 11:20:30 pm"); strcpy(wo[1].fetchflag, "0"); strcpy(wo[1].reason, ""); strcpy(wo[1].accesssource, "rms"); jclass clsh = null; jclass clsc = null; jclass clsw = null; jclass clsr = null; jmethodid midmain = null; jmethodid midcalling = null; jmethodid middispstruct = null; jmethodid middispstructarr = null; jmethodid midretobjfunc = null; jmethodid midctrldetconst = null; jmethodid midwoconst = null; jobject jobjdet = null; jobject jobjretdata = null; jobjectarray jobjwoarr = null; //obtaining classes clsh = env->findclass("helloworld"); clsc = env->findclass("controldetail"); clsw = env->findclass("workorder"); //obtaining method ids if (clsh != null) { midmain = env->getstaticmethodid(clsh, "main", "([ljava/lang/string;)v"); midcalling = env->getstaticmethodid(clsh, "testcall", "(ljava/lang/string;)v"); middispstruct = env->getstaticmethodid(clsh, "displaystruct", "(lcontroldetail;)i"); middispstructarr = env->getstaticmethodid(clsh, "displaystructarray", "([lworkorder;)v"); midretobjfunc = env->getstaticmethodid(clsh, "returnobjfunc", "()ljava/lang/object;"); } else { printf("\nunable find requested class\n"); } if (clsc != null) { //get constructor id controldetail midctrldetconst = env->getmethodid(clsc, "<init>", "(iljava/lang/string;ljava/lang/string;i)v"); } else { printf("\nunable find requested class\n"); } if (clsw != null) { //get constructor id workorder midwoconst = env->getmethodid(clsw, "<init>", "(ljava/lang/string;ljava/lang/string;ljava/lang/string;ljava/lang/string;ljava/lang/string;ljava/lang/string;ljava/lang/string;)v"); } else { printf("\nunable find requested class\n"); } /************************************************************************/ /* call functions using method ids */ /************************************************************************/ if (midmain != null) env->callstaticvoidmethod(clsh, midmain, null); //calling main method. if (midcalling != null) { jstring stringarg = env->newstringutf("\ntestcall:called c program\n"); //calling static method , passing string type parameter env->callstaticvoidmethod(clsh, midcalling, stringarg); } printf("\ngoing call displaystruct\n"); if (middispstruct != null) { if (clsc != null && midctrldetconst != null) { jstring stringargname = env->newstringutf(ctrldetail.name); jstring stringargip = env->newstringutf(ctrldetail.ip); //creating object of controldetail. jobjdet = env->newobject(clsc, midctrldetconst, (jint)ctrldetail.id, stringargname, stringargip, (jint)ctrldetail.port); } if (jobjdet != null && middispstruct != null) env->callstaticintmethod(clsh, middispstruct, jobjdet); //calling method , passing controldetail object parameter } //calling function java , passing structure array it. printf("\n\ngoing call displaystructarray c\n\n"); if (middispstructarr != null) { if (clsw != null && midwoconst != null) { //creating object array contain 2 structures. jobjwoarr = (jobjectarray)env->newobjectarray(2, clsw, env->newobject(clsw, midwoconst, env->newstringutf(""), env->newstringutf(""), env->newstringutf(""), env->newstringutf(""), env->newstringutf(""), env->newstringutf(""), env->newstringutf(""))); //initializing array (int = 0; i<2; i++) { env->setobjectarrayelement(jobjwoarr, i, env->newobject(clsw, midwoconst, env->newstringutf(wo[i].sumserialid), env->newstringutf(wo[i].accessnumber), env->newstringutf(wo[i].actiontype), env->newstringutf(wo[i].effectivedate), env->newstringutf(wo[i].fetchflag), env->newstringutf(wo[i].reason), env->newstringutf(wo[i].accesssource))); } } //calling static method , passing structure array it. if (jobjwoarr != null && middispstructarr != null) env->callstaticvoidmethod(clsw, middispstructarr, jobjwoarr); } //calling static function return object if (midretobjfunc != null) { //calling function , storing return object jobject type variable //returned object structure having 2 fields (string , integer) jobjretdata = (jobject)env->callstaticobjectmethod(clsh, midretobjfunc, null); //get class of object clsr = env->getobjectclass(jobjretdata); //obtaining fields data returned object jint nret = env->getintfield(jobjretdata, env->getfieldid(clsr, "returnvalue", "i")); jstring jstrlog = (jstring)env->getobjectfield(jobjretdata, env->getfieldid(clsr, "log", "ljava/lang/string;")); const char *plog = env->getstringutfchars(jstrlog, 0); printf("\n\nvalues returned object are:\nreturnvalue=%d\nlog=%s", nret, plog); //after using string type data release it. env->releasestringutfchars(jstrlog, plog); } //release resources. int n = jvm->destroyjavavm(); return 0; }
Comments
Post a Comment