title: c++ Plugin -> JNI --> Java Classpath
body: <p>i am trying to create a Plugin for an Application. The Plugin needs to be written in c++. I want to use the Plugin on Windows and on Mac, so it would be great to write the Plugin in Java.</p>
  
  <p>My Problem, there is an other Plugin using Java. Since they are using the same main Application there is already an running JavaVM.</p>
  
  <pre><code>JavaVM *jvm = NULL;
  jsize jvm_count = 0;
  jint res=0;
  
  
  res = JNI_GetCreatedJavaVMs (&amp;jvm, 1, &amp;jvm_count);
  </code></pre>
  
  <p><strong>My Problem:</strong>
  How can i change / modify the Classpath of the existing JavaVM? Or how can i create a new / second JavaVM? </p>
  
  <p>I've tryed to load my jar file via JNI:</p>
  
  <pre><code>/* URL CLASS */
  jclass URLcls;
  URLcls = env-&gt;FindClass("java/net/URL");
  
  /* URL CLASS CONSTRUCTOR*/
  jmethodID URLclsMid;
  URLclsMid = env-&gt;GetMethodID(URLcls, "&lt;init&gt;","(Ljava/lang/String;)V");
  
  /* URL OBJECT */
  jobject URLobj;
  jstr = env-&gt;NewStringUTF("file:/path/to/test/file/test.jar");
  URLobj = env-&gt;NewObject(URLcls, URLclsMid, jstr);
  
  /* URL Array */
  jobjectArray URLArray;
  URLArray = env-&gt;NewObjectArray(1, URLcls, URLobj);
  
  
  
  /*Thread Class*/
  jclass ThreadCLS;
  ThreadCLS = env-&gt;FindClass("java/lang/Thread");
  
  /*Static Method currentThread*/
  jmethodID ThreadCLS_currentThread;
  ThreadCLS_currentThread = env-&gt;GetStaticMethodID(ThreadCLS, "currentThread","()Ljava/lang/Thread;");
  
  /*get current Thread Object*/
  jobject currentThread;
  currentThread = env-&gt;CallStaticObjectMethod(ThreadCLS, ThreadCLS_currentThread);
  
  /* getContextClassLoader method */  
  jmethodID currentThread_getContextClassLoader;
  currentThread_getContextClassLoader = env-&gt;GetMethodID(ThreadCLS, "getContextClassLoader","()Ljava/lang/ClassLoader;");
  
  /* ClassLoader Class */     
  jclass ClassLoaderCLS;
  ClassLoaderCLS = env-&gt;FindClass("java/lang/ClassLoader");
  
  /* get ClassLoader Object */
  jobject classLoader = env-&gt;CallObjectMethod(currentThread, currentThread_getContextClassLoader);
  
  
  /* URLClassLoader Class */
  jclass URLClassLoaderCLS;
  URLClassLoaderCLS = env-&gt;FindClass("java/net/URLClassLoader");
  
  /* Static Method newInstance */
  jmethodID URLClassLoaderCLS_newInstance;
  URLClassLoaderCLS_newInstance = env-&gt;GetStaticMethodID(URLClassLoaderCLS, "newInstance","([Ljava/net/URL;Ljava/lang/ClassLoader;)Ljava/net/URLClassLoader;");
  
  
  /* get new URLClassLoader Instance */
  jobject myURLClassLoaderInstance;
  myURLClassLoaderInstance = env-&gt;CallStaticObjectMethod(URLClassLoaderCLS, URLClassLoaderCLS_newInstance, URLArray, classLoader);
  
  
  /* get setContextClassLoader Method */
  jmethodID currentThread_setContextClassLoader;
  currentThread_setContextClassLoader = env-&gt;GetMethodID(ThreadCLS, "setContextClassLoader","(Ljava/lang/ClassLoader;)V");
  
  /* trying to set the ClassLoader from the current Thread */    
  env-&gt;CallVoidMethod(currentThread, currentThread_setContextClassLoader, myURLClassLoaderInstance);
  
  
  /* get loadClass Method */
  jmethodID loadClass;
  loadClass = env-&gt;GetMethodID(URLClassLoaderCLS, "loadClass","(Ljava/lang/String;)Ljava/lang/Class;");
  
  
  /* get a Class from my test.jar*/
  jstring methodString = env-&gt;NewStringUTF("de.domain.sayHello");
  jclass myClass = (jclass)env-&gt;CallObjectMethod(myURLClassLoaderInstance, loadClass, methodString);
  
  
  /*working till here*/
  
  
  jmethodID myClassMethod;
  myClassMethod = env-&gt;GetMethodID(myClass, "doIt","()Ljava/lang/String;");
  
  if (myClassMethod == NULL) {
      // Method of Class "de.domain.sayHello" not found
  }
  </code></pre>
  
  <p>What can i do to change the Classpath? or to load my jar File?</p>
  
  <p>Thanks
  Lisa</p>