Java uses native files, which are generally decomposed into the following steps:
To declare a local method run Javah using the native keyword in Java code, get the C language header file that contains the method declaration (the C function name in JNI programming is usually related to a Java method with a certain naming convention, which is described later. We use Javah to help us get the name of the method. Generate shared library files in C or C + + to implement the functionality we need, shared library files can be Windows-style. dll files, or UNIX-style. so files To ensure that the virtual machine loads the library the first time it uses the class, the static initialization block is used to load the
Operation Steps:
1.CMD Switch to Directory
CD D:\Works\Android\WorkspaceUnify\rsPolylabel\bin
D:
2. Compile header file
Javah-jni-classpath. -D./jni Rspolylabel.polylabel
Result: Generate h file rspolylabel_polylabel.h to Jni folder
3. Header File Code description
/* Do isn't EDIT this file-it is machine generated/*
#include <jni.h>/
* Header for Class rspolylabel_polyl Abel/
#ifndef _included_rspolylabel_polylabel
#define _included_rspolylabel_polylabel
#ifdef __ Cplusplus
extern "C" {
#endif/*
Class: Rspolylabel_polylabel
* Method: Hellowishchinjni
* Signature: () V
/
jniexport void Jnicall java_rspolylabel_polylabel_ Hellowishchinjni
(jnienv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
5. Use vs Generation Project
Use VS to build a DLL project, write the appropriate CPP file from the header file, and add a table of contents to the Include directory:
C:\Program Files\java\jdk1.8.0_152\include
And
C:\Program Files\java\jdk1.8.0_152\include
7. Load Dynamic Library
Online method of storing DLL files directory to add to the system directory path, and then the egg ...
In the Eclipse program, run System.out.println (System.getproperty ("Java.library.path"));
This row runs complete, output directory
D:\Works\Android\WorkspaceUnify\rsPolylabel\lib\x64;
D:\Works\Android\WorkspaceUnify\rsPolylabel\lib\hdf5
So put the DLL file into the lib\x64 directory and load it successfully.
6. Write code
Add header file contains and JNI included in CPP file
#include <jni.h>/
* Header for Class Rspolylabel_polylabel * *
#include "rspolylabel_polylabel.h"
After that, you can build a dynamic library in a normal way
7. Introduction of Dynamic Library
The test was successful.