• Loading
    • How To Use Java Strings In C?

      Previous Article:- How To Use Java Arrays In C?

      Handling strings in Java differs from handling strings in C. A string in Java is encoded in Unicode. Conversely, C uses the Universal Character Set (UCS) Transformation Format-8 (UTF-8) to represent strings. The native equivalent of a string in Java is jstring, which cannot be used as a string in C.

      Accessing a Java String in C

      Code:
      class StringDemo 
      {
      	String str;
      	//Declaring a native method
      	private native void strDemo();
      	public static void main(String args[])
         	{
          	StringDemo dem  = new StringDemo();
      	    dem.str = "This is a string declared in Java";
      	    //invoking the native method
      	    dem.strDemo();
      	    System.out.println("String in Java:\n");
          	System.out.println("  dem.str = \"" + dem.str + "\"");
        	}
        	static 
      	{
          	    //loading library
      	    System.loadLibrary("StringDemo_lib");
      	}
      }//end class
      This code shows the Java code that declares a native method to access a Java string from C. The implementation of the native code is achieved by loading the corresponding library.

      Header File for the StringDemo File

      Code:
      /* DO NOT EDIT THIS FILE - it is machine generated */
      #include <jni.h>
      /* Header for class StringDemo */
      #ifndef _Included_StringDemo
      #define _Included_StringDemo
      #ifdef __cplusplus
      extern "C" 
      {
      	#endif
      	/*
      	 * Class:     StringDemo
      	 * Method:    strDemo
      	 * Signature: ()V
      	 */
      	JNIEXPORT void JNICALL Java_StringDemo_strDemo (JNIEnv *, jobject);
      	#ifdef __cplusplus
      }
      #endif
      #endif
      This code shows how to generate the header file from the StringDemo class file with the help of the javah tool.

      Implementation of the strDemo Method

      Code:
      #include <stdio.h>
      #include <jni.h>
      #include "StringDemo.h"
      JNIEXPORT void JNICALL 
      Java_StringDemo_strDemo(JNIEnv *exeenv, jobject javaobj)
      {
      	jclass cls = (*exeenv)->GetObjectClass(exeenv, javaobj);
      	jfieldID fid;
      	jstring jstring;
      	const char *string;
      	printf("String accessed in C\n");
      	fid = (*exeenv)->GetFieldID(exeenv, cls, "str", "Ljava/lang/String;");
      	if (fid == 0) 
      	{
          		return;
        	}
        	jstring = (*exeenv)->GetObjectField(exeenv, javaobj, fid);
        	string = (*exeenv)->GetStringUTFChars(exeenv, jstring, 0);
      	printf("dem.s = \"%s\"\n", string);
        	(*exeenv)->ReleaseStringUTFChars(exeenv, jstring, string);
      }
      The above code accesses a string declared in Java code, converts it into an equivalent C string with the help of the GetStringUTFChars method, and then prints it.
    • Currently Active UsersCurrently Active Users

      There are currently 77 users online. 4 members and 73 guests

      Most users ever online was 323, 11-23-2011 at 07:47 AM.

      1. airncnimxkl,
      2. airnqpzarcl,
      3. snehil2529,
      4. vbairmaclsho