CentOS Java C JNI

Source: Internet
Author: User

Using JNI to invoke local code, the entire development process consists of the following steps:
1. Create a Java class (Intarray.java);
2, compile the class with Javac (Generate Intarray.class);
3, using Javah-jni to produce the header file (generate IntArray.h);
4, using the local code to implement the method defined in the header file (write intarray.c);
5, build local dynamic library (generate libintarray.so);
6. Run the program using Java.

create Java class: Intarray.java

ClassIntarray {Private native intSumarray (int[] arr); Public Static voidMain (string[] args) {Intarray P=NewIntarray (); intArr[] =New int[10]; for(inti = 0; I < 10; i++) {Arr[i]=i; } intsum =P.sumarray (arr); System.out.println ("Sum =" +sum); } Static{system.loadlibrary ("Intarray"); }}

Note:
1. Declaring a local method in Java code must have a "native" identifier, a native decorated method that exists only as a declaration in Java code. For example: private native int Sumarray (int[] arr);
2. Before calling the local method, you must first load the local library containing the method. As shown in Intarray.java, in a static block, when the Java VM Initializes a class, this code is executed first, which guarantees that the local library is loaded before the local method is called.

static {
System.loadlibrary ("Intarray");
}

Second, compile the class with Javac (Generate Intarray.class)
Javac Intarray.java
Third, using JAVAH-JNI to produce the header file (Generate IntArray.h)
Javah-jni Intarray

the generated IntArray.h

/*Do not EDIT this file-it are machine generated*/#include<jni.h>/*Header for Class Intarray*/#ifndef _included_intarray#define_included_intarray#ifdef __cplusplusextern "C" {#endif/** Class:intarray * Method:sumarray * Signature: ([i) I*/jniexport jint jnicall java_intarray_sumarray (jnienv*, Jobject, Jintarray); #ifdef __cplusplus}#endif#endif

Iv. using native code to implement the method defined in the header file (write intarray.c)
Copy IntArray.h into intarray.c and make the following modifications for INTARRAY.C:
1. Add header file: #include "IntArray.h"
2. Remove the following sentences
#ifndef _included_intarray
#define _included_intarray
#endif
3. Implement the method defined in the header file:

intarray.c

/*Do not EDIT this file-it are machine generated*/#include<jni.h>#include"IntArray.h"/*Header for Class Intarray*/#ifdef __cplusplusextern "C" {#endif/** Class:intarray * Method:sumarray * Signature: ([i) I*/jniexport jint jnicall java_intarray_sumarray (jnienv*env, Jobject obj, Jintarray arr) {Jint buf[Ten] = {0}; Jint I=0, sum =0; (*ENV)->getintarrayregion (env, arr,0,Ten, BUF); for(i =0; I <Ten; i++) {sum+=buf[i];} returnsum;} #ifdef __cplusplus}#endif

V. Build a local dynamic library (generate libintarray.so)
Generate a local dynamic library based on local code (INTARRAY.H,INTARRAY.C), with the following command:

gcc -i/home/oracle/jdk1. 8. 0_05/include-i/HOME/ORACLE/JDK1. 8. 0_05/include/linux/-fpic-shared-o libintarray.so intarray.c

Where-I is followed by the Java Include folder address, please according to your specific Java version and the installation path to make the corresponding changes;
The pic behind-F indicates that the symbols in the generated library are location-independent (pic is position independent Code), for pic, refer to this article
Http://www.gentoo.org/proj/en/hardened/pic-guide.xml

-shared represents shared, shared library suffix name. So can be considered as the abbreviation for shared object;
-O libintarray.so, which can be understood as output Libintarray.so library after compilation.

Vi. using Java to run the program
Java Intarray

The following results may occur:

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. <clinit> (intarray.java:15)

Analysis of the exception hint that we previously generated the shared library is not in the system default shared library path, the program cannot find the shared library error.
There are two solutions:
1. Temporarily specify the path to the shared library libintarray.so.
Export ld_library_path=.: $LD _library_path Note: This method only works in the current session window and switches to a different terminal window, you need to reassign the shared library path.  You can also add directly to the environment variable: ld_library_path=.: $ORACLE _home/lib:/usr/lib64:/usr/lib:/usr/local/lib64:/usr/local/lib Command line use: source ~/.bash_profile to make the currently added environment variable effective

2, the runtime plus the parameter specifies the path of the shared library libintarray.so.
Java-djava.library.path=. Intarray Note:-D: Sets the system properties for the Java platform. At this point JAVAVM can find the library at its current location.

With any of these methods, you can get the correct results:
Java Intarray
Sum = 45

--------------------------------------------

Article from: http://www.2cto.com/os/201108/101048.html

CentOS Java C 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.