1. Writing a Java class with a native declaration method
2. Compiling Java classes using the Javac command
3. Use the Java-jni className to generate a header file with the suffix named. h
4. Implementing local methods using other languages (c, C + +)
5. Generate a dynamic-link library of files written by local methods
One code: (Do not take the package path)
/**
* Native-0study
*
* @author Xuedui.zhao
* @create 2018-04-23
*/
public class HelloWorld {
public native void Hello ();
static{
System.loadlibrary ("Hello");
}
public static void Main (string[] args) {
New HelloWorld (). hello ();
}
}
Two:
In terminal execution: Javac Helloworld.java
The Helloworld.class file is generated
Three: Use Java-jni className to generate header file with suffix. h
/* 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
IV: Create a HELLOWORLDIMPL.C file
#include "jni.h"
#include "HelloWorld.h"
#include <stdio.h>
Jniexport void Jnicall Java_helloworld_hello (jnienv *env,jobject obj) {
printf ("Hello world!\n");
Return
}
V: Generate a dynamic-link library of files written by local methods
Gcc-dynamiclib-i/library/java/javavirtualmachines/jdk1.8.0_162.jdk/contents/home/include Helloworldimpl.c-o Libhello.jnilib
An exception is thrown at this point:
Helloworldimpl.c:1:10:fatal error: ' jni.h ' File not found
#include "jni.h"
^~~~~~~
1 Error generated.
VI: sudo cp/library/java/javavirtualmachines/jdk1.8.0_162.jdk/contents/home/include/darwin/jni_md.h/library/java/ Javavirtualmachines/jdk1.8.0_162.jdk/contents/home/include
Seven: Ls-al
Drwxr-xr-x 7 Xuedui.zhao Staff 224 4 23 21:35.
Drwxr-xr-x 9 Xuedui.zhao Staff 288 4 23 11:52..
-rw-r--r--1 Xuedui.zhao Staff 442 4 14:07 Helloworld.class
-rw-r--r--1 Xuedui.zhao Staff 377 4 14:07 HelloWorld.h
-rw-r--r--1 Xuedui.zhao Staff 276 4 14:06 Helloworld.java
-rw-r--r--1 Xuedui.zhao Staff 4 21:27 HELLOWORLDIMPL.C
-rwxr-xr-x 1 Xuedui.zhao Staff 8400 4 21:35 libhello.jnilib
Eight: Terminal execution: Java HelloWorld
Hello world!
Mac under Java JNI (Java Native Interface) c