hello
I'm trying to call a java method from c++ code.
this is the first time i'm using JNI so just copied the helloworld code from net.
my problem is when i try to compile the c code it gives me error
undefined reference to `JNI_CreateJavaVM'
collect2: error: ld returned 1 exit status
the code is
#include <stdio.h>
#include <jni.h>
JNIEnv* create_vm() {
JavaVM* jvm;
JNIEnv* env;
JavaVMInitArgs args;
JavaVMOption options[1];
args.version = JNI_VERSION_1_6;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=/home/girish/Desktop/jnitest";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;
JNI_CreateJavaVM(&jvm, (void **)&env, &args);
return env;
}
void invoke_class(JNIEnv* env) {
jclass helloWorldClass;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0;
helloWorldClass = (*env)->FindClass(env, "HelloWorld");
mainMethod = (*env)->GetStaticMethodID(env, helloWorldClass, "main", "([Ljava/lang/String;)V");
applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), NULL);
applicationArg0 = (*env)->NewStringUTF(env, "From-C-program");
(*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);
(*env)->CallStaticVoidMethod(env, helloWorldClass, mainMethod, applicationArgs);
}
int main(int argc, char **argv) {
JNIEnv* env = create_vm();
invoke_class( env );
}
I'm compiling this code with
gcc -o test -I /usr/lib/jvm/java-7-openjdk-i386/include/ -I /usr/lib/jvm/java-7-openjdk-i386/include/linux/ -L /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/ -ljvm -L /usr/bin/java test.c
so my first question is how to fix this? i've made sure that libjvm.so is present in /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/ .
and my second question is libjvm.so is present in three directories /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/client/ , /usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/server/ and
/usr/lib/jvm/java-7-openjdk-i386/jre/lib/i386/jamvm/ which one I need to compile this code.
can some buddy help me?
thank you.
girish.