The problem of garbled characters passing in utf8 from JNI has plagued me for a long time. Today, I finally found a solution.
Since env-> newstringutf supports modified UTF-8, this API cannot be used from JNI
To Java.
// Returnutf8.java </P> <p> View plaincopy to clipboardprint? </P> <p> public class returnutf8 {</P> <p> static {</P> <p> system. loadlibrary ("returnutf8jni"); </P> <p >}</P> <p> Public static native byte [] Method1 (); </P> <p> Public static native object method2 (); </P> <p> Public static void main (string [] ARGs) {</P> <p> try {</P> <p> byte [] buffer = returnutf8.method1 (); </P> <p> string S1 = new string (buffer, "UTF-8"); </P> <p> system. out. println ("S1 =" + S1); </P> <p> string S2 = returnutf8.method2 (). tostring (); </P> <p> system. out. println ("S2 =" + S2); </P> <p >}catch (Java. io. unsupportedencodingexception e) {</P> <p> E. printstacktrace (); </P> <p >}</P> <p> public class </P> <p> returnutf8 {</P> <p> static {</P> <p> system. loadlibrary ("returnutf8jni"); </P> <p >}</P> <p> Public static native byte [] Method1 (); </P> <p> Public static native object method2 (); </P> <p> Public static void main (string [] ARGs) {</P> <p> try {</P> <p> byte [] buffer = returnutf8.method1 (); </P> <p> string S1 = new string (buffer, "UTF-8"); </P> <p> system. out. println ("S1 =" + S1); </P> <p> string S2 = returnutf8.method2 (). tostring (); </P> <p> system. out. println ("S2 =" + S2); </P> <p >}catch (Java. io. unsupportedencodingexception e) {</P> <p> E. printstacktrace (); </P> <p >}</P> <p> </P> <p>
# Javac returnutf8.java
# Java returnutf8
A returnutf8.h header file will be generated here
// Returnutf8.cpp </P> <p> View plaincopy to clipboardprint? </P> <p> # include "returnutf8.h" </P> <p> jniexport jbytearray jnicall java_returnutf8_method1 </P> <p> (jnienv * ENV, jclass CLs) </P> <p >{</P> <p> jbyte buffer [] =/* utf8 encoding buffer */</P> <p> jbytearray bytes = env-> newbytearray (sizeof (buffer )); </P> <p> env-> setbytearrayregion (bytes, 0, sizeof (buffer), buffer); </P> <p> return bytes; </P> <p >}</P> <p> jniexport jbytearray jnicall java_returnutf8_method2 </P> <p> (jnienv * ENV, jclass CLs) </P> <p >{</P> <p> jbyte buffer [] =/* utf8 encoding buffer */</P> <p> jbytearray bytes = env-> newbytearray (sizeof (buffer )); </P> <p> env-> setbytearrayregion (bytes, 0, sizeof (buffer), buffer ); </P> <p> jchar encoding_name [] = {'U', 't', 'F', '-', '8 ',''}; </P> <p> jstring encoding = env-> newstring (encoding_name, 5 ); </P> <p> jclass thestringclass = env-> findclass ("ljava/lang/string ;"); </P> <p> jmethodid mid = env-> getmethodid (thestringclass, "", "([bljava/lang/string;) V "); </P> <p> jobject result = env-> newobject (thestringclass, mid, bytes, encoding); </P> <p> return result; </P> <p >}</P> <p> # include </P> <p> "returnuf8.h" </P> <p> jniexport jbytearray jnicall java_returnuf8_method1 </ p> <p> (jnienv * ENV, jclass CLs) </P> <p >{</P> <p> jbyte buffer [] =/* utf8 encoding buffer */</P> <p> jbytearray bytes = env-> newbytearray (sizeof (buffer )); </P> <p> env-> setbytearrayregion (bytes, 0, sizeof (buffer), buffer); </P> <p> return bytes; </P> <p >}</P> <p> jniexport jbytearray jnicall java_returnutf8_method2 </P> <p> (jnienv * ENV, jclass CLs) </P> <p >{</P> <p> jbyte buffer [] =/* utf8 encoding buffer */</P> <p> jbytearray bytes = env-> newbytearray (sizeof (buffer )); </P> <p> env-> setbytearrayregion (bytes, 0, sizeof (buffer), buffer ); </P> <p> jchar encoding_name [] = {'U', 't', 'F', '-', '8 ',''}; </P> <p> jstring encoding = env-> newstring (encoding_name, 5 ); </P> <p> jclass thestringclass = env-> findclass ("ljava/lang/string ;"); </P> <p> jmethodid mid = env-> getmethodid (thestringclass, "", </P> <p> "([bljava/lang/string ;) V "); </P> <p> jobject result = env-> newobject (thestringclass, mid, bytes, </P> <p> encoding ); </P> <p> return result; </P> <p >}</P> <p>
# G ++-shared-I/usr/lib/JVM/Java-1.5.0-sun/include-I/usr/lib/JVM/Java-1.5.0-sun/include/Linux returnutf8.cpp-O libreturnutf8jni. so
# Export ld_library =.
# Java returnutf8