title: Passing String argument from c to java using jni
body: <p>I am trying to call java method from C, but getting below error</p>
  
  <blockquote>
    <p>Status after JNI_CreateJavaVM=&lt;0><br>
    restCall method found:: Avinash Kumar  </p>
    
    <p>Execution error : file ''<br>
    error code: 114, pc=0, call=1, seg=0<br>
    114     Attempt to access item beyond bounds of memory (Signal 11)</p>
  </blockquote>
  
  <p>Target is to send String as an argument from C to Java function.</p>
  
  <p>Below is the code :</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;jni.h&gt;
  
  #define ENV (*env)
  
  enum eConst
  {
     MAX_Options     =  21   ,
     MAX_LREF        = 120  ,
     PARSING_ERROR   = -10  ,
     ERROR           = -999 ,
     EXCEPTION_ERROR = -100 ,
     OK              = 1    ,
     YES             = 'Y'  ,
     NO              = 'N'
  };
  
  int main()
  {
      jint    jRet;
      jstring   jszRes;
      static JavaVM *jvm = NULL;
      static JNIEnv *env  = NULL ;
  
      JavaVMInitArgs vm_args;
      JavaVMOption options[MAX_Options-1];
      int optionCount=0;
      options[optionCount++].optionString = "-verbose:jni,class,gc";
      char *path="user/work/avikumar/";
      char *class_path="user/work/avikumar/";
      char path_option[2000]={'\n'};
      sprintf(path_option,"-Djava.class.path=%s/MyClass.class:%s/jersey-core-1.19.jar:%s/jersey-client-1.19.jar:%s/javax.ws.rs-api-2.0-m02.jar:.",class_path,path,path,path);
  
      options[optionCount++].optionString = path_option;
      options[optionCount++].optionString = "-Xms128m";
      options[optionCount++].optionString = "-Xmx512m";
      options[optionCount++].optionString = "-Xss8m";
  
      vm_args.options = options;
      vm_args.nOptions = optionCount;
      vm_args.version = JNI_VERSION_1_6;
      vm_args.ignoreUnrecognized = JNI_FALSE;
  
      jRet = JNI_CreateJavaVM(&amp;jvm,(void**)&amp;env,(void*)&amp;vm_args);
      printf("Status after JNI_CreateJavaVM=&lt;%d&gt;\n",jRet);
      if (jRet &lt; 0)
      {
         return(-4);
      }
  
      if( ENV-&gt;EnsureLocalCapacity(env, MAX_LREF) &lt; 0)
      {
        printf( "\n out of memory. Program terminated\n");
        (*jvm)-&gt;DestroyJavaVM(jvm);
        return(-5);
      }
  
      jclass jlocClass;
      jlocClass= ENV-&gt;FindClass(env, "MyClass");
      if (jlocClass == NULL || (*env)-&gt;ExceptionOccurred(env))
      {
         printf("Can not load the main class\n");
         return (-6);
      }
  
      jmethodID restCall_method;
      restCall_method = ENV-&gt;GetMethodID(env, jlocClass,"restCall","(Ljava/lang/String;)V");
      if( restCall_method == NULL)
      {
        printf("restCall method not found\n");
        return (-7);
      }
  
      char *inp =  "Avinash Kumar";
      printf("restCall method found:: %s\n",inp);
      //jstring jstr = (*env)-&gt;NewStringUTF(env,inp);
  
      jstring jstr1=(*env)-&gt;NewStringUTF(env, "Avinash Kumar");
  
      ENV-&gt;CallVoidMethod(env, jlocClass, restCall_method,jstr1);
      (*jvm)-&gt;DestroyJavaVM(jvm);
      return 0;
  } 
  </code></pre>
  
  <p>MyClass.java</p>
  
  <pre class="lang-java prettyprint-override"><code>class MyClass
  {
     public static void main(String args[])
     {
      System.out.println("Hello Avinash\n");
     }
  
     public void restCall(String inp)
     {
      System.out.println(inp+ " :: Hello Avinash from restCall\n");
     }
  
  }
  </code></pre>