Today in the ArrayList source see the System.arraycopy this method, but this method does not have Java implementation.
After a query lookup, it is known that the native is a Java call to the C language to implement the operation of JNI. And then just write
A JNI demo.
1 writing a Java file with native
public class HelloWorld {
public native void Hello ();
static{
System.loadlibrary ("Hello");
}
public static void Main (string[] args) {
New HelloWorld (). hello ();
}
}
2 command-line compilation Javac Helloworld.java
3 command-line compilation Javah-jni HelloWorld generate HelloWorld.h file
/* Don't EDIT this file-it are machine generated */
#include <jni.h>
/* Header for class HelloWorld */
#ifndef _included_helloworld
#define _included_helloworld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:helloworld
* Method:hello
* Signature: () V
*/
jniexport void Jnicall Java_helloworld_hello
(JNIENV *, jobject);
#ifdef __cplusplus
}
#endif
#endif
4 Writing the implementation of C language HELLOWORLD.C
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
Jniexport void Jnicall Java_helloworld_hello (jnienv *env,jobject obj) {
printf ("Hello world!\n");
Return
}
5 writing. C implementation. o File
gcc-fpic-d_reentrant-i/library/java/javavirtualmachines/jdk1.7.0_75.jdk/contents/home/include-i/library/java/ Javavirtualmachines/jdk1.7.0_75.jdk/contents/home/include/darwin-c HELLOWORLD.C
Two addresses the first one is jni.h the second one is jni_md.h as shown.
6 Compile and build the local library file. Dylib
gcc-shared Helloworld.o-o Libhello.dylib
7 Running Java files Helloword
All of the above files are best placed together to allow permission.
Mac implementation of JNI demo