JNI instance 1

Source: Internet
Author: User

1. Write a Java program that contains a native function.

 

package cn.vicky.jni;/** * * @author Vicky.H */public class HelloWorld {    // 1.need a native method    public native void sayHello();    }

2. Compile the Java program.

[Root @ localhost SRC] # tree
.
'-- CN
'-- Vicky
'-- JNI
'-- Helloworld. Java

3 directories, 1 file
[Root @ localhost SRC] # javac cN/Vicky/JNI/helloworld. Java
[Root @ localhost SRC] # tree
.
'-- CN
'-- Vicky
'-- JNI
| -- Helloworld. Class
'-- Helloworld. Java

3 directories, 2 files
[Root @ localhost SRC] # javah CN. Vicky. JNI. helloworld
[Root @ localhost SRC] # tree
.
| -- CN
| '-- Vicky
| '-- JNI
| -- Helloworld. Class
| '-- Helloworld. Java
'-- Cn_vicky_jni_helloworld.h

3 directories, 3 files

If the preceding operations are performed, a corresponding C/C ++ header file is generated.

 

3. Write a C/C ++ dynamic file library.

The dynamic file library in. So format is still written using netbeans + centos. The project directory is as follows:

Cn_vicky_jni_helloworld.h is the header file generated by the javah command. JNI. h and jni_md.h are:

[Root @ localhost SRC] # cd/usr/Java/default/include/
[Root @ localhost include] # tree
.
| -- Classfile_constants.h
| -- Jawt. h
| -- Jdwptransport. h
| -- JNI. h
| -- Jvmti. h
'-- Linux
| -- Jawt_md.h
'-- Jni_md.h

1 directory, 7 files

Copy the corresponding file.

Compile the header file: cn_vicky_jni_helloworld_impl.cpp

#include "cn_vicky_jni_HelloWorld.h"#include <iostream>using namespace std;JNIEXPORT void JNICALL Java_cn_vicky_jni_HelloWorld_sayHello (JNIEnv *env, jobject obj){    cout << "hello world" << endl;}

Is a simple program that outputs "Hello World. Generate a program: Obtain the dynamic link library of libjni_01_cpp.so. To only use this dynamic link library, we need to copy it to the/usr/lib file.

 

4. Java calls libjni_01_cpp.so

Continue to write the helloworld. Java program

package cn.vicky.jni;/** * * @author Vicky.H */public class HelloWorld {    // 1.need a native method    public native void sayHello();        public static void main(String[] args) {        System.loadLibrary("jni_01_cpp");        new HelloWorld().sayHello();    }}

 

[Vicky @ localhost SRC] $ javac cN/Vicky/JNI/helloworld. Java
[Vicky @ localhost SRC] $ tree
.
| -- CN
| '-- Vicky
| '-- JNI
| -- Helloworld. Class
| '-- Helloworld. Java
'-- Cn_vicky_jni_helloworld.h

3 directories, 3 files
[Vicky @ localhost SRC] $ Java CN. Vicky. JNI. helloworld
Hello World

Program Execution, complete. Delete the dynamic link library used for testing:

[Root @ localhost SRC] # rm/usr/lib/libjni_01_cpp.so
RM: Do you want to delete the common file "/usr/lib/libjni_01_cpp.so "? Y

 

Certificate ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

The above is used by JNI in Linux. For Windows, it is basically the same. When the jni_01_cpp.dll file is generated, we need to address the directory where jni_01_cpp.dll is located, baby to the path in windows. For example:

Then run the helloworld program.

 

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.