Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C
JNIEnv* create_vm(JavaVM ** jvm) {
    
    JNIEnv *env;
    JavaVMInitArgs vm_args;

    JavaVMOption options; 
    //Path to the java source code     
    options.optionString = "-Djava.class.path=G:\\Workspace"; 
    vm_args.version = JNI_VERSION_1_6; //JDK version. This 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 to Launch JVM\n");       
    return env;
}


Please help me out what to do at this point.

Thanks in advance.
Posted
Updated 8-Mar-13 10:38am
v2
Comments
[no name] 8-Mar-13 15:05pm    
Do you think that it might be beneficial for you to describe what it is that you want to do instead of making people guess?
Sergey Alexandrovich Kryukov 11-Mar-13 14:06pm    
I had to remove two of your post, complete abuse.

Please stop posting non-answers as "solution". It can give you abuse reports which eventually may lead to cancellation of your CodeProject membership.
Comment on any posts, reply to available comments, or use "Improve question" (above).
Also, keep in mind that members only get notifications on the post sent in reply to there posts.

The fact that you even self-accepted your "answer" formally is outrageous. If you do this persistently, it's a sure ways to loose your membership for abuse. For not, please consider yourself warned.

—SA

Maybe the glaringly obvious needs to be stated. It seems you need to include a header file that contains a definition of _JNI_CreateJavaVM(). This looks like the java native interface. If you are compiling and linking to a library, you need to tell the compiler/linker where to find the things you are calling.

The details are platform specific, IDE specific and command line specific. More details required to help you further.
 
Share this answer
 
Comments
sandeep vihaari 11-Mar-13 12:19pm    
Hi H.Brydon,

Thank you for the reply.

I am using LabWindows/CVI to invoke the java method in C. This is my code to call a simple java method. I have included jni.h

#include
#include
#include
#include


JNIEnv* create_vm(JavaVM ** jvm) {

JNIEnv *env;
JavaVMInitArgs vm_args;

/*JavaVMOption options;
//Path to the java source code
options.optionString = "-Djava.class.path=G:\\Workspace";
vm_args.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args.nOptions = 4;
vm_args.options = &options;
vm_args.ignoreUnrecognized = 0; */

JavaVMOption options[4];

options[0].optionString = "-Djava.compiler=NONE"; /* disable JIT */
options[1].optionString = "-Djava.class.path=G:\\Workspace"; /* user classes */
options[2].optionString = "-Djava.library.path=C:\\Program Files (x86)\\Java\\jdk1.6.0_25\\jre\\bin\\client"; /* set native library path */
options[3].optionString = "-verbose:jni"; /* print JNI-related messages */

vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = 4;
vm_args.ignoreUnrecognized = 0;

int ret = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);
if(ret < 0)
printf("\nUnable to Launch JVM\n");
return env;
}


int main()
{

JNIEnv *env;
JavaVM * jvm;
env = create_vm(&jvm);
if (env == NULL)
return 1;

jclass clsH=NULL;
jmethodID midMain = NULL;
jmethodID midRetObjFunc = NULL;

//Obtaining Classes
clsH = (*env)->FindClass(env,"Test");


//Obtaining Method IDs
if (clsH != NULL)
{
midMain = (*env)->GetStaticMethodID(env,clsH, "main", "()V");
}
else
{
printf("\nUnable to find the requested class\n");
}

/************************************************************************/
/* Now we will call the functions using the their method IDs */
/************************************************************************/
if(midMain != NULL)
(*env)->CallStaticVoidMethod(env,clsH, midMain, NULL); //Calling the main method.

//Release resources.
int n = (*jvm)->DestroyJavaVM(jvm);
return 0;
}
Check here[^] for JNI help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900