Reference code for Java local method call (JNI) -- convert string to char * and convert char * to string.

Source: Internet
Author: User

First, create test. java, and then use javac test. java compiled to test. class, and then use javah-JNI test to get test. h, and then create test. C and include "test. H "and implement the corresponding local method. After completing these steps, use the following command to compile test. c into a dynamic link library:

Gcc-C test. c

Gcc-shared-fpci-O libtest. So test. o

Note: you must first set JNI. H and jni_md.h are copied to the current directory, and JNI. change <jni_md.h> in H to "jni_md.h", and set test. change <jni_md.h> in H to "jni_md.h ".

The source code is as follows:

/***********Test.java***************/public class Test{    static{     System.loadLibrary("test");    }    public Test(){        System.out.println(getString());        String str="Hello,BBBBBBBBBBBBBBA";        showString(str);    }    public native String getString();    public native void showString(String str);        public static void main(String[] args){         new Test();    }}

/***************Test.h****************//* DO NOT EDIT THIS FILE - it is machine generated */#include "jni.h"/* Header for class Test */#ifndef _Included_Test#define _Included_Test#ifdef __cplusplusextern "C" {#endif/* * Class:     Test * Method:    getString * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_Test_getString  (JNIEnv *, jobject);/* * Class:     Test * Method:    showString * Signature: (Ljava/lang/String;)V */JNIEXPORT void JNICALL Java_Test_showString  (JNIEnv *, jobject, jstring);#ifdef __cplusplus}#endif#endif

/************* Test. c ****************/# include "test. H "# include <string. h> # include <stdio. h> # include <stdlib. h> jstring cstr2jstring (jnienv * ENV, const char * Pat) {// defines the Java string class strclass jclass strclass = (* env)-> findclass (ENV, "ljava/lang/string;"); // gets the constructor of the Java string class method string (byte [], string, converts a local byte [] array to a new string jmethodid ctorid = (* env)-> getmethodid (ENV, strclass, "<init> ", "([bljava/lang/string;) V"); // create a byte array jbytearray bytes = (* env)-> newbytearray (ENV, (jsize) strlen (PAT); // convert char * To byte array (* env)-> setbytearrayregion (ENV, bytes, 0, (jsize) strlen (PAT ), (jbyte *) Pat); // sets the string and saves the language type. It is used to convert the byte array to the string parameter jstring encoding = (* env)-> newstringutf (ENV, "gb2312"); // converts the byte array to a Java string and outputs return (jstring) (* env)-> newobject (ENV, strclass, ctorid, bytes, encoding);} Char * jstring2cstr (jnienv * ENV, jstring jstr) {char * RTN = NULL; jclass clsstring = (* env)-> findclass (ENV, "Java/lang/string"); jstring strencode = (* env)-> newstringutf (ENV, "gb2312"); jmethodid mid = (* env)-> getmethodid (ENV, clsstring, "getbytes", "(ljava/lang/string;) [B"); jbytearray Barr = (jbytearray) (* env)-> callobjectmethod (ENV, jstr, mid, strencode); jsize Alen = (* env)-> getarraylength (ENV, Barr); jbyte * Ba = (* env)-> getbytearrayelements (ENV, Barr, jni_false ); if (Alen> 0) {RTN = (char *) malloc (Alen + 1); // new char [Alen + 1]; memcpy (RTN, Ba, Alen ); RTN [Alen] = 0;} (* env)-> releasebytearrayelements (ENV, Barr, Ba, 0); Return RTN;} jniexport jstring jnicall java_test_getstring (jnienv * ENV, jobject OBJ) {char * mychar = "hello, everyone"; return cstr2jstring (ENV, mychar);} jniexport void jnicall java_test_showstring (jnienv * ENV, jobject OBJ, jstring Str) {char * mychar = jstring2cstr (ENV, STR); printf ("--> % S <-- \ n", mychar );}

Compiling and running environment:

Root @ Ubuntu:/home/KYC # javac-version
Javac 1.6.0 _ 27

Root @ Ubuntu:/home/KYC # Java-version
Java version "1.6.0 _ 27"
Openjdk Runtime Environment (icedtea6 1.12.3) (6b27-1.12.3-0ubuntu1 ~ 12.04)
Openjdk server VM (build between 0-b12, mixed mode)

Program running:

----------------)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.