title: calling java function from c using jni
body: <p>I'm writing a simple program to call a Java function from my C program.</p>
  
  <p>Following is my code:</p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/ipc.h&gt;
  #include &lt;sys/shm.h&gt;
  #include &lt;sys/mman.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  
  JNIEnv* create_vm() {
      JavaVM* jvm;
      JNIEnv *env;
      JavaVMInitArgs vm_args;
  
      JavaVMOption options[1];
  
      options[0].optionString - "-Djava.class.path=/home/chanders/workspace/Samples/src/ThreadPriorityTest";
      vm_args.version = JNI_VERSION_1_6;
      vm_args.nOptions = 1;
      vm_args.options = &amp;options;
      vm_args.ignoreUnrecognized = JNI_FALSE;
  
      JNI_CreateJavaVM(&amp;jvm, (void**)&amp;env, &amp;vm_args);
      return env;
  }
  
  void invoke_class(JNIEnv* env) {
      jclass helloWorldClass;
      jmethodID mainMethod;
      jobjectArray applicationArgs;
      jstring applicationArg0;
  
      helloWorldClass = (*env)-&gt;FindClass(env, "InvocationHelloWorld");
      mainMethod = (*env)-&gt;GetStaticMethodID(env, helloWorldClass, "main", "([Ljava/lang/String;)V");
  
      applicationArgs = (*env)-&gt;NewObjectArray(env, 1, (*env)-&gt;FindClass(env, "java/lang/String"), NULL);
      applicationArg0 = (*env)-&gt;NewStringUTF(env, "From-C-program");
      (*env)-&gt;SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);
      (*env)-&gt;CallStaticVoidMethod(env, helloWorldClass, mainMethod, applicationArgs);
  }
  
  int main() {
      JNIEnv* env = create_vm();
      invoke_class(env);
  }
  </code></pre>
  
  <p>I'm compiling the above program using:
  gcc -o invoke -I$JAVA_HOME/include/  -I$JAVA_HOME/include/linux -L$JAVA_HOME/jre/lib/amd64/server/ ThreadPriorityTest.c </p>
  
  <p>and i'm getting the following error:</p>
  
  <pre><code>/tmp/ccllsK5O.o: In function `create_vm': ThreadPriorityTest.c:(.text+0x35): undefined reference to `JNI_CreateJavaVM' collect2: ld returned 1 exit status
  </code></pre>
  
  <p>I'm not really sure what is causing this problem </p>
  
  <p>UPDATE 1</p>
  
  <p>Included the -ljvm in the command line and then got a <code>undefined reference to FUNCTION_NAME</code>
  I'm running it on Rhel 6.2 </p>