Respect for the original: http://www.cnblogs.com/Alandre/p/4456719.html
First, the understanding native namely Jni,java native Interface
All a language, all hope is pure. For example, to solve a certain program is like to write in this language alone. Java platform has a user and local C code interoperability API, called Java Native Interface (Java Local interface).
Second, using Java to call C's "Hello,jni"
We need to follow the easy steps:
1. Create a Java class that contains a native method and a method to load the library loadLibrary. The Hellonative.java code is as follows:
123456789101112131415 |
public class HelloNative
{
static
{
System.loadLibrary("HelloNative");
}
public static native void sayHello();
@SuppressWarnings("static-access")
public static void main(String[] args)
{
new HelloNative().sayHello();
}
}
|
First, the Masons let everyone notice is the native method, the loading library to the back also work. The native keyword tells the compiler (in fact, the JVM) that the method is called externally, and this refers to C. If you run this code directly, the JVM will tell you: "A Java Exception has occurred." The console output is as follows:
12345 |
Exception in thread "main" java.lang.UnsatisfiedLinkError: no HelloNative in java.library.path at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) at HelloNative.< clinit >(HelloNative.java:5) |
This is when the program uses it, and the virtual machine says it doesn't know how to find SayHello. The following can be written manually, natural masons are used
2. Run Javahto get the C Declaration header file containing the method . h
The Masons will Hellonative.java, simply Javac Javah,
You get the following HelloNative.h file :
123456789101112131415161718192021 |
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <
jni.h
>
/* Header for class HelloNative */
#ifndef _Included_HelloNative
#define _Included_HelloNative
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloNative
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloNative_sayHello
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
|
Jni.h this file, in/%java_home%include
3, according to the header file, write c to implement the local method .
Here we simply implement this SayHello method as follows:
1234567 |
#include "HelloNative.h" #include < stdio.h > JNIEXPORT void JNICALL Java_HelloNative_sayHello { printf("Hello,JNI"); } |
4, build the DLL shared library , and then Java Program Load Library, call can.
On Windows, MinGW GCC runs as follows
1 |
gcc -m64 -Wl,--add-stdcall-alias -I"C:\Program Files\Java\jdk1.7.0_71\include" -I"C:\Program Files\Java\jdk1.7.0_71\include\include\win32" -shared -o HelloNative.dll HelloNative.c |
-m64 indicates that the build DLL library is 64-bit. Then run hellonative:
Finally, you can see the console print as follows:
Third, JNI call C flowchart
Java Fundamentals: Understanding & Understanding Keywords Native combat