title: JNI "env->GetStaticMethodID()" crashed program
body: <p>I'm trying to call a Java function from C++.
  This is my code so far:</p>
  
  <pre><code>#include &lt;jni.h&gt;
  
  typedef struct JavaVMCreationResult {
      JavaVM* jvm;
      JNIEnv* env;
  } JVMCreationResult;
  
  JVMCreationResult* CreateJavaVM() {
      JavaVM* jvm;
      JNIEnv* env;
  
      JavaVMInitArgs args;
  
      JavaVMOption opts[1];
      opts[0].optionString = "-Djava.class.path=C:\\MyJavaClasses";
  
      args.version = JNI_VERSION_1_6;
      args.nOptions = 1;
      args.options = opts;
      args.ignoreUnrecognized = JNI_TRUE;
  
      JNI_GetDefaultJavaVMInitArgs(&amp;args);
  
      JNI_CreateJavaVM(&amp;jvm, (void **) &amp;env, &amp;args);
  
      JavaVMCreationResult* cres;
      cres-&gt;jvm = jvm;
      cres-&gt;env = env;
  
      return cres;
  }
  
  int main() {
      JVMCreationResult* cres = CreateJavaVM();
      JavaVM* jvm = cres-&gt;jvm;
      JNIEnv* env = cres-&gt;env;
  
      jclass cls = env-&gt;FindClass("Main");
      jmethodID mid = env-&gt;GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V"); // the evil line
  }
  </code></pre>
  
  <p>I'm using Code::Blocks with MinGW GCC on Windows 7.<br>
  The last line in the main() function crashes the Program, but the compiler does not complain about anything. (Commenting out the <code>jmethodID mid = env-&gt;GetSta...</code> line makes the program "not crashing")<br>
  I have uses <code>javap -s Main</code> to obtain the right method signature, also the class is a valid Java class.</p>
  
  <p>Can you tell me why the Program crashes ? This example is just shown everywhere on the Internet but it doesn't work for me. :(</p>
  
  <p>This is the Java class:</p>
  
  <pre><code>public class Main {
      public static void main(String[] args) {
          System.out.println("This is from Java !");
      }
  }
  </code></pre>
  
  <h2>SOLUTION</h2>
  
  <p>I wouldn't have thought it, it seems unlogic to me that the program wasn't crashing earlier when the struct wasn't initialized. But this was really the issue.<br>
  This is the full and working code !</p>
  
  <pre><code>#include &lt;jni.h&gt;
  
  #ifndef null
  #define null NULL
  #endif
  
  typedef struct JavaVMCreationResult {
      JavaVM* jvm;
      JNIEnv* env;
  } JVMCreationResult;
  
  JVMCreationResult* CreateJavaVM() {
      JavaVM* jvm;
      JNIEnv* env;
  
      JavaVMInitArgs args;
  
      JavaVMOption opts[1];
      opts[0].optionString = "-Djava.class.path=C:\\Users\\Claudia\\Desktop";
  
      args.version = JNI_VERSION_1_6;
      args.nOptions = 1;
      args.options = opts;
      args.ignoreUnrecognized = JNI_TRUE;
  
      JNI_GetDefaultJavaVMInitArgs(&amp;args);
  
      JNI_CreateJavaVM(&amp;jvm, (void **) &amp;env, &amp;args);
  
      JVMCreationResult* cres = new JVMCreationResult();
      cres-&gt;jvm = jvm;
      cres-&gt;env = env;
  
      return cres;
  }
  
  int main() {
      JVMCreationResult* cres = CreateJavaVM();
      JavaVM* jvm = cres-&gt;jvm;
      JNIEnv* env = cres-&gt;env;
  
      jclass cls = env-&gt;FindClass("Main");
      if (cls) {
          printf("Yes !\n");
      }
      else {
          printf("No !\n");
      }
      jmethodID mid = env-&gt;GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
      env-&gt;CallStaticVoidMethod(cls, mid);
  
      printf("At end of Program.");
  }
  </code></pre>