Java uses JNI

Source: Internet
Author: User

The core of Java calling C + + is the conversion of variable types, and the Java int is the same as int in C + +, the conversion is relatively simple. If you think of string in Java, the processing is more complicated.

Jniexport jstring jnicall java_com_dd_lib_chinesetopinyin (jnienv *env, Jclass, jstring src_jstr)

{

Try not to use char *a = new char[100]; Such a declaration may result in various libc.so.6 errors, as much as possible to allow all memory to be reclaimed by the system.

/* Start with new and delete to manage memory, appeared c [libc.so.6+0x7a5db] unsigned long+0xa5b, c [LIBC.SO.6+0X1E9E0] [email protected] @GLIBC_PRI Vate+0x1e9e0 and so on errors, error hints can only be accurate to the function, should be memory errors, so use Java calls C + + to try to let the system to manage memory. */

Char Src_nc[srclen];

try{

Convert Java string to C + + string, where Src_char is a pointer to a byte array in Src_jstr

const char * Src_char = Env->getstringutfchars (Src_jstr, false);

strcpy (Src_nc,src_char);

To release the SRC_JSTR after the string is removed

Env->releasestringutfchars (Src_jstr, Src_char);

} catch (Exception &e) {

cout << e.what () << Endl;

}

.....................

String ret_str = "return value";

String to be converted to Java on return

Jstring rtstr = Env->newstringutf (Ret_str.c_str ());

return rtstr;

}

Java uses JNI

Related Article

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.