The entire process of JNI calling simple instance operations in Linux

Source: Internet
Author: User
Development Environment: Linux (Ubuntu11.04) + JDK7 instance description: JNI is used to call local code to calculate the sum of Int arrays. JNI is used to call local code, the entire development process includes the following steps: 1. create a Java class (IntArray. java )...

Development Environment: Linux (Ubuntu 11.04) + JDK 7
Example: use JNI to call local code to calculate the sum of Int arrays.


To call local code using JNI, the entire development process includes the following steps:
1. create a Java class (IntArray. java );
2. use javac to compile the class (generate IntArray. class );
3. use javah-jni to generate the header file (generate IntArray. h );
4. use local code to implement the methods defined in the header file (write IntArray. c );
5. Compile and generate a local dynamic library (libIntArray. so );
6. run the program in Java.


1. create a Java class (IntArray. java)
Class IntArray {
Private native int sumArray (int [] arr );
Public static void main (String [] args ){
IntArray p = new IntArray ();
Int arr [] = new int [10];
For (int I = 0; I <10; I ++ ){
Arr [I] = I;
}

Int sum = p. sumArray (arr );
System. out. println ("Sum =" + sum );
}

Static {
System. loadLibrary ("IntArray ");
}
} Note:
1. declare in Java code that the local method must have a "native" identifier and a native modification method. in Java code, the method only exists as a declaration. Example: private native int sumArray (int [] arr );
2. before calling a local method, you must first load the local library containing the method. such as IntArray. as shown in java, it is placed in a static block. when Java VM initializes a class, it first executes this part of code, which ensures that the local library is loaded before calling the local method.
Static {
System. loadLibrary ("IntArray ");
}

II. use javac to compile the class (generate IntArray. class)
Javac IntArray. java
3. use javah-jni to generate the header file (generate IntArray. h)
Javah-jni IntArray
Generate IntArray. h
/* Do not edit this file-it is machine generated */
# Include
/* Header for class IntArray */

# Ifndef _ Included_IntArray
# Define _ Included_IntArray
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: IntArray
* Method: sumArray
* Signature: ([I) I
*/
JNIEXPORT jint JNICALL Java_IntArray_sumArray
(JNIEnv *, jobject, jintArray );

# Ifdef _ cplusplus
}
# Endif
# Endif

4. use local code to implement the methods defined in the header file (compile IntArray. c)
Copy IntArray. h to IntArray. c and make the following changes to IntArray. c:
1. add the header file: # include "IntArray. h"
2. remove the following sentence:
# Ifndef _ Included_IntArray
# Define _ Included_IntArray
# Endif
3. implement the methods defined in the header file

The specific code of IntArray. c is as follows:
/* Do not edit this file-it is machine generated */
# Include
/* Header for class IntArray */
# Include "IntArray. h"

# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: IntArray
* Method: sumArray
* Signature: ([I) I
*/
JNIEXPORT jint JNICALL Java_IntArray_sumArray
(JNIEnv * env, jobject obj, jintArray arr)
{
Jint buf [10] = {0 };
Jint I = 0, sum = 0;
 
(* Env)-> GetIntArrayRegion (env, arr, 0, 10, buf );
 
For (I = 0; I <10; I ++)
{
Sum + = buf [I];
}
 
Return sum;
}
# Ifdef _ cplusplus
}
# Endif

5. Compile and generate a local dynamic library (generate libIntArray. so)
Generate a local dynamic library based on the local code (IntArray. h, IntArray. c). The command is as follows:
Gcc-I/usr/lib/jvm/java-7-sun/include/-I/usr/lib/jvm/java-7-sun/include/linux/-fPIC- shared-o libIntArray. so IntArray. c note:
Here,-I is followed by the include folder address of java. please change it based on your specific java version and installation path;
-The PIC after f indicates that the symbols in the generated library are not related to the Position (PIC is the Position Independent Code). for details about the PIC, refer to this article.
Introduction "> http://www.gentoo.org/proj/en/hardened/pic-guide.xml"> Introduction to Position Independent Code
-Shared indicates sharing. the shared library extension. so can be considered as the abbreviation of shared object;
-O libIntArray. so can be understood as the libIntArray. so Library output after compilation.

6. run the program in Java
Java IntArray

The following results may appear:
Snowdream @ snowdream :~ $ Java IntArray
Exception in thread "main" java. lang. UnsatisfiedLinkError: no IntArray in java. library. path
At java. lang. ClassLoader. loadLibrary (ClassLoader. java: 1860)
At java. lang. Runtime. loadLibrary0 (Runtime. java: 845)
At java. lang. System. loadLibrary (System. java: 1084)
At IntArray. (IntArray. java: 15)
Analysis exception prompt: the shared library we generated earlier is not in the system's default shared library path, and the program cannot find the shared library. an error is reported.
There are two solutions:
1. temporarily specify the path of the shared library libIntArray. so.
Export LD_LIBRARY_PATH =.: $ LD_LIBRARY_PATH note: This method is only valid in the current session window. to switch to another terminal window, you need to specify the shared library path again.

2. add a parameter to the runtime to specify the path of the shared library libIntArray. so.
Java-Djava. library. path =. IntArray note:-D: set the system attributes of the Java platform. In this case, JavaVM can find the library at the current location.

You can obtain the correct running result using any of the above methods:
Snowdream @ snowdream :~ $ Java IntArray
Sum = 45

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.