[Android] Simple implementation process of JNI in Linux, androidjni

Source: Internet
Author: User

[Android] Simple implementation process of JNI in Linux, androidjni

First, write a java program

HelloJNI. java

Public class HelloJNI {public native void displayHelloJNI (); // all native keywords are modified to the local declaration static {System. loadLibrary ("MyLib"); // load local library} public static void main (String [] args) {new HelloJNI (). displayHelloJNI ();}}

 

Declare native METHOD: If you want to use a method as a local method, you must declare this method as native and cannot implement it.
System. loadLibrary ("MyLib"); load dynamic library
We can understand as follows: Our method displayHelloWorld () is not implemented, but we use it directly below, so we must initialize it before use) static blocks are generally used for loading. Note that the parameter "hello" of System. loadLibrary (); is the name of the dynamic library.

Compile
Javac HelloJNI. java gets HelloJNI. class

Generate. h header file
Javah HelloJNI
Obtain HelloJNI. h.

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class HelloJNI */#ifndef _Included_HelloJNI#define _Included_HelloJNI#ifdef __cplusplusextern "C" {#endif/* * Class:     HelloJNI * Method:    displayHelloJNI * Signature: ()V */JNIEXPORT void JNICALL Java_HelloJNI_displayHelloJNI  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif

 

This H file is equivalent to our interface in java. Here we declare a Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject); method, and then implement this method in our local method, that is to say, the method name we use when writing a C/C ++ program must be consistent with the method name here.


Compile a local method to implement the same method as the method name declared in the header file generated by the javah command.
Test. c

#include<stdio.h>#include"jni.h"#include"HelloJNI.h"JNIEXPORT void JNICALL Java_HelloJNI_displayHelloJNI  (JNIEnv *env, jobject obj){    printf("this is a JNI test program!\n");    return;}

 

Jni. h (this file can be found under the % JAVA_HOME %/include folder) file introduction, because the types of JNIEnv and jobject in the program are defined in this header file; in addition, the HelloWorld. h header file introduction (I understand this as follows: It is equivalent that we can declare an interface when writing a java program. Here we will introduce HelloWorld. h. The methods declared in the header file are implemented. Of course not necessarily ).

Compile test. c into the current directory of the Dynamic Link Library

gcc -shared -I /home/du/jdk1.7.0_71/include -I /home/du/jdk1.7.0_71/include/linux test.c -o libMyLib.so

 

Obtain the libMyLib. so Dynamic Link Library.

In HelloJNI. java, the LoadLibrary method loads "MyLib ",
The generated Library is libMyLib. This is what Linux links stipulate,
A library must be lib + Library name +. so. You only need to provide the database name for the link.

Run HelloJNI. java

java HelloJNI

Exception in thread "main" java. lang. UnsatisfiedLinkError: no MyLib in java. library. path

 

java -Djava.library.path=. HelloJNI

 

Run or

export LD_LIBRARY_PATH=/home/du/test_code:$LD_LIBRARY_PATHjava HelloJNI

To obtain the output.

 

Refer:

Getting started with JNI in Linux (Ubuntu)

Use of JNI in Linux

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.