title: What is libJvm.so, and when is it built?
body: <p>I'm a bit surprised that I have to post a question that looks rather trivial and easy to look up.... but it apparently isn't.</p>
  
  <p>Because it looks that my <code>jni.h</code> and the <code>libJvm.so</code> library are out of sync (or so I suspect), which shows up trying to load my Java application as a library into the C++ environment on Linux(with <code>JNI_CreateJVM()</code> and all that), I wonder which could be the culprit. The <code>jni.h</code> is just standard, containing all normal modules.... the <code>libJvm.so</code> however... I cannot see what the contents are.... I don't even know what it really is and how/ where it is built. </p>
  
  <p>Can anybody give me an explanation/answer to this question?</p>
  
  <p>P.S. the problems don't have to do with library path and related issues... the <code>libJvm.so</code> can be found. It is just hard to understand what goes wrong as long as I don't exeactly know all the components I'm working with.... </p>
  
  <p>Hereunder an update, following on comments that came in in reactions on my question (it is a very long update, because I decided it might be insightful to include the code that I'm working with)...</p>
  
  <p>The code I'm working with (and experiencing the problem with), after the code I also say something about the environment/settings and circumstances....:</p>
  
  <pre><code>#include &lt;jni.h&gt;
  #include "jvm.h"
  #include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;dlfcn.h&gt;
  
  using namespace std;
  
  //Create type for pointer to the JNI_CreateJavaVM function
  typedef jint (*CreateJvmFuncPtr) (JavaVM**, void**, JavaVMInitArgs*); 
  
  //New method returns pointer to the JNI_CreateJavaVM function
  CreateJvmFuncPtr findCreateJvm() { 
  CreateJvmFuncPtr createJvm = NULL;
  
  void* jvmLib = dlopen("/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.71.x86_64/jre/lib/amd64/server/libjvm.so", RTLD_LAZY); //Get handle to jvm shared library
  char* error = dlerror(); //Check for errors on dlopen
  
  if(jvmLib == NULL || error != NULL) {
      printf("FailedToLoadJVM\n");
  }
  
  //Load pointer to the function within the shared library
  createJvm = (CreateJvmFuncPtr) dlsym(jvmLib, "JNI_CreateJavaVM");
  
  error = dlerror();
  if(error != NULL) {
      printf("Success\n");
  }
  
  return createJvm;
  </code></pre>
  
  <p>}</p>
  
  <pre><code>int main() {
  
  // load and initialize JVM and JNI interface, in a test setting where JVM is "passive"
  // (called/loaded as .dll) and the JNI env is not available yet from a Java/JVM call
  // (which would be the caste in the normal setup/configuration).
  //int jniLoadJvmAsDll()
  //{
     JavaVM *jvm;                      // Pointer to the JVM (Java Virtual Machine)
     JNIEnv *env;                      // Pointer to native interface
         //======== prepare loading of Java VM: assign init arguments for JVM init =========
     JavaVMInitArgs vm_args;                        // Initialization arguments
  //       JavaVMOption* vmOptions = new JavaVMOption[1];   // JVM invocation options
     JavaVMOption vmOptions[4];
  
                                                          // (=&gt; comparable to lib path for System.load(libPath/libName)
  
     vmOptions[0].optionString = "-Djava.compiler=NONE";           /* disable JIT */
     vmOptions[1].optionString = "-Djava.class.path=/home/adminuser/workspace_Unit_Test_Java_Cpp/Unit_Test_Jni_Java/bin"; // which Java class to load
     vmOptions[2].optionString = "-Djava.library.path=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.71.x86_64/jre/lib/amd64/;/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.71.x86_64/jre/lib/amd64/server";
     vmOptions[3].optionString = "-verbose:jni";                   /* print JNI-related messages */
     vm_args.version = JNI_VERSION_1_6;             // minimum Java version
     vm_args.nOptions = 1;                          // number of options
     vm_args.options = vmOptions;
     vm_args.ignoreUnrecognized = false;     // invalid options make the JVM init fail
  
         //=============== load and initialize Java VM and JNI interface =============
     // old code: jint retCrJvm = JNI_CreateJavaVM(&amp;jvm, (void**)&amp;env, &amp;vm_args);  // YES !!
     //New code:
     CreateJvmFuncPtr createJVM = findCreateJvm();
     printf("findCreateJVM() returned 0x%x\n", createJVM);
  
     jint retCrJvm = createJVM(&amp;jvm, (void**)&amp;env, &amp;vm_args);
     //End new code
     delete vmOptions;    // we then no longer need the initialization options.
     if (retCrJvm != JNI_OK) {
            // TO DO: error processing...
           cin.get();
           exit(EXIT_FAILURE);
     }
  
         //====================handle multithread situatiation ========================
     JavaVM * curVM = NULL;           // JVM in current thread, should contain loaded VM
                                      // otherwise we're not in the main thread
     JNIEnv *curEnv;
     int jvmLoadFailed = 1;
  
     // handle situation that we're not in the main thread (current thread isnt't main thread)
     if (jvm == NULL) {
         env-&gt;GetJavaVM( &amp;curVM ); // JVM should contain current/main thread JVM, otherwise
                                   // we're not in the main thread
  
         int vmStat = curVM-&gt;GetEnv((void **)&amp;curEnv, JNI_VERSION_1_6);
         if (vmStat == JNI_EDETACHED)  //We are on a different thread, attach
             curVM-&gt;AttachCurrentThread((void **) &amp;curVM, NULL);
         if( curVM == NULL )
             return jvmLoadFailed;  //Can't attach to java, bail out
     }
  
        //=============== Display JVM version =======================================
     cout &lt;&lt; "JVM load (as .so/.dll) succeeded: Version ";
     jint ver = env-&gt;GetVersion();
     std::cout &lt;&lt; ((ver&gt;&gt;16)&amp;0x0f) &lt;&lt; "."&lt;&lt;(ver&amp;0x0f) &lt;&lt; endl;
  
     // TO DO: add the code that will use JVM &lt;============  (see next steps)
  
     // insert the JNITools separate methods here.... for each separate graphical structure
     // initially a test class is inserted.....
     jclass clsTestPrint = env-&gt;FindClass("TestPrint");  // try to find the class
     if(clsTestPrint == NULL) {
         cerr &lt;&lt; "ERROR: class not found !";
     }
     jmethodID ctor = env-&gt;GetMethodID(clsTestPrint, "&lt;init&gt;", "()V");  // find an object constructor for this class
     if(ctor == NULL) {
         cerr &lt;&lt; "ERROR: constructor not found !" &lt;&lt; endl;
     }
     else {
         cout &lt;&lt; "Object succesfully constructed !"&lt;&lt;endl;
         jobject prtObj = env-&gt;NewObject(clsTestPrint, ctor); // invoke the object constructor: create print object
         if (prtObj) {
              jmethodID show = env-&gt;GetMethodID(clsTestPrint, "showId", "()V");
              if(show == NULL)
                  cerr &lt;&lt; "No showId method !!" &lt;&lt; endl;
              else env-&gt;CallVoidMethod(prtObj, show);
         }
     }
  
  
     jvm-&gt;DestroyJavaVM();
     cin.get();
     return 0;
  </code></pre>
  
  <p>//}</p>
  
  <p>}</p>
  
  <p>Where "dlopen" and "dlsym" of course replace the "loadLibrary()" and "getProcaddress()" calls you would use on Windows to 1. load a .dll and get a handle to the appropriate JNI procedure... but I'm doing this on Linux, therefore the "equivalent translations"....</p>
  
  <p>The "multithread" decoration is not really relevant here.... added it for possible future purposes.</p>
  
  <p>After some experiments I succeeded in configuaring the build settings so that also the libjvm.so is found and within that the API JNI method JNI_CreateJavaVM()....
  For that I added (on Linux, using Eclipse) the following "GCC C++ Linker" settings:
  -- => "Libraries" => "Libraries (-l)" => "Jvm" and
  => "Library Search Path (-L)" => 
  "/usr/lib/jvm/java-1.7.0.openjdk-1.7.0.71.x86_64/jre/lib/amd64/server"</p>
  
  <p>-- => "Shared Library Settings" => "Shared Object Name " => "libjvm.so"</p>
  
  <p>Other settings gave errors during compilation/linking.</p>
  
  <p>With this, the environment can be built. The debugger is started and stops in main and I can step througt the C++ code as usual until.... I reach the JNI_CreateJavaVM() invocation. At that time the following error occurs.</p>
  
  <p>relocation error: /home/adminuser/workspace_Unit_Test_Java_Cpp/Unit_Test_Jni_Cpp/Debug/Unit_Test_Jni_Cpp: symbol JNI_CreateJavaVM, version SUNWprivate_1.1 not defined in file libjvm.so with link time reference</p>
  
  <p>I have hereby strong suspicions that either:
   1. the jni header and the library (libJvm.so) are out of sync, or
   2. the class path or lib path do not contain the correct value's
  3. the Java .so (.dll) loaded (with "dlopen") is not what it should be.... </p>
  
  <p>Any idea's which value's to use?</p>
  
  <p>This is the "background of my question: to determine wheter the jni and libJvm.so content are out of sync, is it very convenient to know what it is and where it is about....</p>