This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Linking JNI code to JVM.dll file.......Getting Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Linking JNI code to JVM.dll file.......Getting Error" Watch "Linking JNI code to JVM.dll file.......Getting Error" New topic

Linking JNI code to JVM.dll file.......Getting Error

vimalkeshubhai chaudhari

Joined: Dec 14, 2009
Posts: 3
I am trying to link JVM.dll with my JNI code to create a runtime environment........
But when I am executing this code, the function JNI_CreateJavaVM returns -1 which specifies that JVM creation is failed......

Can anyone please suggest where I am going wrong?

I am posting the JNI code :

int JavaAPI(ServerParameter* servParams, int i) {
char* (*lib_func)() = 0;
jint (*lib_func2)()= 0;
JNIEnv *env= NULL;
JavaVM *jvm= NULL;
JavaVMInitArgs vm_args;
JavaVMOption options[5];
jclass cls;
jmethodID mid;
jstring jstr1,jstr2,jstr3,jstr4,jresult;
jobjectArray args;
const char* tempstr;
jsize bufLen = 1;
jsize nVMs=0;
jint res, resCheckVM;
void *thr_args;
char clspath[400]="-Djava.class.path=";

resCheckVM = JNI_GetCreatedJavaVMs(&jvm, bufLen, &nVMs);
if( resCheckVM < 0 || nVMs < 1 )
vm_args.version = JNI_VERSION_1_6;
vm_args.options = options;
vm_args.nOptions = 5;
vm_args.ignoreUnrecognized = JNI_FALSE;
options[0].optionString = "-Xms4M";
options[1].optionString = "-Xmx64M";
options[2].optionString = "-Xss512K";
options[3].optionString = "-Xoss400K";
options[4].optionString = clspath;

res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args); //here JNI_CreateJavaVM is returning -1,due to which JVM creation is failing
if (res < 0)
fprintf(stderr, "Failed to Create JVM.\n");
res = (jvm)->AttachCurrentThread((void**)&env, NULL);
if (res < 0)
fprintf(stderr, "Failed attaching current thread to JVM.\n");

cls = (env)->FindClass(servParams[i].lib);
//cls = (env)->FindClass("SSOClass");
if (cls == 0)
fprintf(stderr, "Can't find Prog class %s\n",servParams[i].lib);
return 1;

David Newton

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.

Nothing to do with testing; moving to more appropriate forum.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19654

And I'll move this further along to Other JSE/JEE APIs which is where we discuss JNI.

How To Ask Questions How To Answer Questions
Don't get me started about those stupid light bulbs.
subject: Linking JNI code to JVM.dll file.......Getting Error
Similar Threads
Invoking JVM through C++ code
JNI_CreateJavaVM returns error
JNI Invocation Debugging
JNI_CreateJavaVM returns with -1