title: Calling java methods in C++ using JNI
body: <p>I need to call java methods from a c++ application. I've followed the instructions in these tutorials: <a href="http://hildstrom.com/projects/jni/index.html" rel="nofollow">http://hildstrom.com/projects/jni/index.html</a> <a href="http://www.codeproject.com/Articles/22881/How-to-Call-Java-Functions-from-C-Using-JNI" rel="nofollow">http://www.codeproject.com/Articles/22881/How-to-Call-Java-Functions-from-C-Using-JNI</a>. here is my code: </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "../Header/jni.h"
  
  JNIEnv* create_vm(JavaVM **jvm)
  {
      JNIEnv* env;
      JavaVMInitArgs args;
      JavaVMOption options;
      args.version = JNI_VERSION_1_6;
      args.nOptions = 1;
      options.optionString = "-Djava.class.path= C:\\Users\\vhsn\\workspace-SP\\helloWorld\\src\\helloWorld";
      args.options = &amp;options;
      args.ignoreUnrecognized = 0;
      int rv;
      rv = JNI_CreateJavaVM(jvm, (void**)&amp;env, &amp;args);
      if (rv &lt; 0 || !env)
          printf("Unable to Launch JVM %d\n",rv);
      else
          printf("Launched JVM! :)\n");
      return env;
  }
  
  void invoke_class(JNIEnv* env)
  {
      jclass hello_world_class;
      jmethodID main_method;
      jmethodID square_method;
      jmethodID power_method;
      jint number=20;
      jint exponent=3;
      hello_world_class = env-&gt;FindClass("HelloWorld");
      main_method = env-&gt;GetStaticMethodID(hello_world_class, "main", "([Ljava/lang/String;)V");
      square_method = env-&gt;GetStaticMethodID(hello_world_class, "square", "(I)I");
      power_method = env-&gt;GetStaticMethodID(hello_world_class, "power", "(II)I");
      printf("carregou metodos");
      env-&gt;CallStaticVoidMethod(hello_world_class, main_method, NULL);
      printf("%d squared is %d\n", number,
          env-&gt;CallStaticIntMethod(hello_world_class, square_method, number));
      printf("%d raised to the %d power is %d\n", number, exponent,
          env-&gt;CallStaticIntMethod(hello_world_class, power_method, number, exponent));
  }
  
  int main(int argc, char **argv)
  {
      JavaVM *jvm;
      JNIEnv *env;
      env = create_vm(&amp;jvm);
      if(env == NULL)
          return 1;
      invoke_class(env);
      return 0;
  }
  </code></pre>
  
  <p>Im compilling it with minGW, linking with jvm.lib located at "C:\Program Files (x86)\Java\jdk1.7.0_79\lib".</p>
  
  <p>It compiles successfuly, but when I execute it only prints "Launched JVM! :)" and then crashes.
   When I try to debug, it crashes in the first GetStaticMethodID call. Im thinking it is not loading the HelloWorld.class properly but I have no clue why.</p>
  
  <p>Does anyone ever faced a similar problem or have an idea what the problem might be?</p>
  
  <p>I am on a windows machine 64bits, I am using java 32 bits because it didnt compile with jvm.lib from java 64 bits.</p>
  
  <p>EDIT:</p>
  
  <p>I have added a </p>
  
  <pre><code>if(env-&gt;ExceptionOccurred()){
              env-&gt;ExceptionDescribe();
          }
  </code></pre>
  
  <p>and it throws a java.lang.NoClassDefFoundError. I think im not clearly understanding what to put in</p>
  
  <pre><code>options.optionString =  "-Djava.class.path=C:\\Users\\vhsn\\workspace\\AAPlugin\\Debug"; 
  </code></pre>
  
  <p>Is it the path for the .class file or the .jar file?</p>
  
  <p>Thank You Very Much!   </p>