2 Replies Latest reply: Nov 17, 2012 4:57 PM by EJP RSS

    GetStringUTFChars results in fatal error

    974625
      Today I started developing a simple JNI extension for the very first time. So I fully expect that the error might be on my side, but after searching and reading quite some examples, I can't figure it out. It looks as I do everything correct at least.

      I set up a netbeans 6.7 environment on OpenIndiana, as described on http://cnd.netbeans.org/docs/jni/nb6-linux/beginning-jni-linux.html

      I succesfully created a class that was capable of running the Hello World in the end.

      Then I created a function in java that looks like this;
      public native long nativeFunction(String name);

      I recreated the headers and I got a function that looks like this in my header;
      JNIEXPORT jlong JNICALL Java_application_nativeFunction
      (JNIEnv *, jobject, jstring);

      Then I implemented it like this in my c-file;
      JNIEXPORT jlong JNICALL Java_application_nativeFunction (JNIEnv *env, jobject obj, jstring name) {
      jlong ret;
      const char str = (env)->GetStringUTFChars(env, name,0);
      ret=100;

      (*env)->ReleaseStringUTFChars(env,str,name);
      return ret;
      }

      If I comment the lines with GetStringUTFChars and ReleaseStringUTFChars in them, the java and c parts compile perfectly and if I call the function nativeFunction I nicely get a return of 100 with the still added nativeprint after it (hello world from c). When I add the previously mentioned lines, both parts compile nicely, but the run of the java-result end in this;

      #
      # A fatal error has been detected by the Java Runtime Environment:
      #
      # SIGSEGV (0xb) at pc=0xfe6ec42e, pid=3148, tid=2
      #
      # JRE version: 6.0_26-b03
      # Java VM: Java HotSpot(TM) Client VM (20.1-b02 mixed mode solaris-x86 )
      # Problematic frame:
      # V [libjvm.so+0xec42e] jni_GetStringUTFChars+0xe2
      #
      # An error report file with more information is saved as:
      # /home/michael/NetBeansProjects/javaproject/hs_err_pid3148.log
      #
      # If you would like to submit a bug report, please visit:
      # http://java.sun.com/webapps/bugreport/crash.jsp
      #

      Hello world from C



      So the application doesn't crash, but for some reason it won't do the very needed JNI-part. Any ideas?
        • 1. Re: GetStringUTFChars results in fatal error
          974625
          Just fixed my stupid buggy code. I called the private "native" Javafunction from a wrapping public function within that class. Apparently it called the private function with a string that was initialised as null. Very stupid, I admit. This really makes a case for checking within my private native functions and within my c-code for correct input.
          • 2. Re: GetStringUTFChars results in fatal error
            EJP
            If you look at the JDK code you will see that invariably there is a Java wrapper for each native function that does all the sanity checking it possibly can, and maybe also rearranges the output, so that the native function is only ever called with correct arguments. It's a lot easier to do the checks in Java and if you forget one it's a lot quicker to just add it to the Java code.