[Java JNI quick learning tutorial] and Java JNI quick learning

Source: Internet
Author: User

[Java JNI quick learning tutorial] and Java JNI quick learning

 1. Introduction to JNI

JNI is the abbreviation of Java Native Interface, which is a Java Local Interface.

Problem Source: because it is difficult to compile underlying applications in Java, it is difficult to be competent in some Java programs with high real-time requirements (the real-time requirements are not yet involved, and such topics need to be studied ).

Solution: Using JNI in Java, you can call an existing local library (C/C ++ develops any program and class libraries related to the system), which greatly facilitates Java development.

2. JNI quick learning tutorial

2.1 questions:

Use JNI to write a piece of code to implement the string_Java_Test_helloworld (JNIEnv * env, jclass cls, jstring j_str) function. Add hello to the string j_str ("world") and return the result.

2.2 Problem Solving Process:

I. Compile the Test. java class:

 1 public class Test{ 2     // native interface 3     public native String helloworld(String text); 4      5     public static void main(String[] args){ 6         // Load dynamic library 7         System.loadLibrary("Test2"); 8         Test ts = new Test(); 9         String text = ts.helloworld("world");10         System.out.println(text);11     }12 }

Note:

1. Load the dynamic Class Library: System. loadLibrary ("Test2"); [Test2.dll is loaded in Windows, Test2.so is loaded in Linux]

II. Compile the Test. java File

Enter cmd and enter the command> javac Test. java

III. Generate the Test. h file

Enter cmd and enter the command> javah Test

The content of the Test. h file is as follows:

 1 /* DO NOT EDIT THIS FILE - it is machine generated */ 2 #include <jni.h> 3 /* Header for class Test */ 4  5 #ifndef _Included_Test 6 #define _Included_Test 7 #ifdef __cplusplus 8 extern "C" { 9 #endif10 /*11  * Class:     Test12  * Method:    helloworld13  * Signature: (Ljava/lang/String;)Ljava/lang/String;14  */15 JNIEXPORT jstring JNICALL Java_Test_helloworld16   (JNIEnv *, jobject, jstring);17 18 #ifdef __cplusplus19 }20 #endif21 #endif

Note:

1. function declaration, fixed format: JNIEXPORT; return type: jstring; JNI call: JNICALL; Java _ complete name_method name: Java_Test_helloworld;

2. function parameters: Call the jni. h encapsulated function pointer: JNIEnv; Java class itself: jobject; input parameter of the Java file: jstring.

IV. Compile the C language file Test2.c to enable the Test class to call the dynamic link library function:

 1 #include "Test.h" 2 #include <string.h> 3  4 JNIEXPORT jstring JNICALL Java_Test_helloworld 5   (JNIEnv *env, jobject obj, jstring string){ 6     const char* str = (*env)->GetStringUTFChars(env,string,0); 7     char cap[128]; 8     cap[0] = 'h'; 9     cap[1] = 'e';10     cap[2] = 'l';11     cap[3] = 'l';12     cap[4] = 'o';13      14     strcat(cap,str);15       16     (*env)->ReleaseStringUTFChars(env,string,0);17     return (*env)->NewStringUTF(env,cap);18   }

Note:

1. Because Java uses double-byte characters, the C language itself is a single-byte character, you need to use (* env)-> GetStringUTFChars () to convert the string between Java and C;

2. GetStringUTFChars () and NewStringUTF (). The first is to convert UTF8 to the c encoding format, and the second is to return a UTF8 String Based on the C string;

3. ReleaseStringUTFChars () is used to release objects. There are virtual machines in Java for garbage collection, but these objects must be manually recycled in C language; otherwise, memory leakage may occur.

V. Compile and run

Compile:

Enter cmd and enter the command> gcc-I "D: \ Program Files \ Java \ jdk1.8.0 _ 45 \ include"-I "D: \ Program Files \ Java \ jdk1.8.0 _ 45 \ include \ win32 "-- share Test2.c-o Test2.dll

Run:

Enter cmd and enter the command> java Test

The running result is as follows:

Helloworld

3. Summary:

Step 1: Compile the Java class (Test. java) with the native method, and use the javac tool to compile the Java class (Generate Test. class );

Step 2: Use javah to generate the header file (Test. h) corresponding to the native method );

Step 3: Use C/C ++ to implement the corresponding header file (Test2.c) and compile it into a dynamic link library (Test2.so ).

Running Environment: Windows 64-bit operating system, JDK 1.8, mingw64 (GCC ).

4. Reference:

Http://www.cnblogs.com/icejoywoo/archive/2012/02/22/2363709.html

[Reprinted this article please note the original Address Source: http://www.cnblogs.com/eesijian/p/5843879.html]

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.