title: undefined reference to `JNI_CreateJavaVM' linux
body: <p>I'm trying to get familiar with the JNI API but can't get a sample c++ program to compile.</p>
  
  <p>Here is the command I'm using to compile and below that is the program I'm trying to compile. The error I get is:</p>
  
  <pre><code>/tmp/cczyqqyL.o: In function `main':
  /home/nc/Desktop/jni/simple/ctojava/callJava.cpp:16: undefined reference to `JNI_CreateJavaVM'
  </code></pre>
  
  <p>Compile:</p>
  
  <pre><code>g++ -g -I/usr/lib/jvm/java-7-oracle/include/ -I/usr/lib/jvm/java-7-oracle/include/linux/ -L/usr/bin/java -L/usr/lib/jvm/java-7-oracle/jre/lib/amd64/server/ -ljvm callJava.cpp
  </code></pre>
  
  <p>C++:</p>
  
  <pre><code>#include &lt;jni.h&gt; /* where everything is defined */
  
  int main(){
      JavaVM *jvm; /* denotes a Java VM */
      JNIEnv *env; /* pointer to native method interface */
  
     JavaVMInitArgs vm_args;
     JavaVMOption options[1];
     options[0].optionString = "-Djava.class.path=/home/nc/Desktop/jni/simple/ctojava/";
     vm_args.version = JNI_VERSION_1_6;
     vm_args.options = options;
     vm_args.nOptions = 1;
     vm_args.ignoreUnrecognized = JNI_FALSE;
  
     /* Create the Java VM */
     int res = JNI_CreateJavaVM(&amp;jvm, (void **)&amp;env, &amp;vm_args); // this is what it can't find
  
     /* invoke the Main.test method using the JNI */
     jclass cls = env-&gt;FindClass("Hello");
     jmethodID mid = env-&gt;GetStaticMethodID(cls, "staticInt", "(I)I");
     env-&gt;CallStaticVoidMethod(cls, mid,10);
  
     /* We are done. */
     jvm-&gt;DestroyJavaVM();
  }
  </code></pre>
  
  <p>I've searched for this issue and tried every solution I've found but still I get the same error... Any help is greatly appreciated!</p>
  
  <p>EDIT: Joni's answer below works (depending on your compiler). In case someone else finds this: when running the compiled output don't forget LD_LIBRARY_PATH=_path_to_your_libjvm.so_ or it will not be able to find that lib at runtime.</p>
  
  <pre><code>LD_LIBRARY_PATH=/usr/lib/jvm/java-7-oracle/jre/lib/amd64/server ./a.out
  </code></pre>