Simple use of JNI
Take a simple HelloWorld program to introduce the most basic method of use of JNI:
1) First there must be a HelloWorld.java.
This is the primary file, including the Java declaration of the local method, a main function, there is a static code segment, which is used to import the required dynamic connection library (which is .dll in Windows).
code show as below:
//Helloworld.java
Class helloworld {
Public native void displayhelloWorld (); // Note the keyword native, which means that this method is implemented with local methods.
The Static {// The static code segment is imported into Hello.dll.
System.loadLibrary ("Hello");
}
Public static void main (String [] args) {// calls this class's DisplayHelloWorld method, (of course, the method is actually implemented in C language)
New helloworld (). DisplayHelloWorld ();
}
}
2) Compile helloWorld.java.
Use the statement as:,
Javac HelloWorld.java
3) Generate one .h file using the javah command.
Use the statement as:,
Javah HelloWorld
This is the header file that implements the C file of the DisplayHelloWorld () method. The file name is HelloWorld.h code as follows:
/ * Do Not Edit this file - IT is Machine Generated * /
#include
/ * Header for class helloworld * /
#ifndef _included_helloWorld
#define _included_helloWorld
#ifdef __cplusplus
Extern "C" {
#ENDIF
/ *
* Class: HelloWorld
* Method: DisplayHelloworld
* Signature: () V
* /
JNIEXPORT VOID JNICALL JAVA_HELLOWORLD_DISPLAYHELLOWORLD
(JNIENV *, JOBJECT);
#ifdef __cplusplus
}
#ENDIF
#ENDIF
It can be seen that this file is mainly to declare the method of the method that needs to be implemented in the C file. This statement and the Java file HelloWorld.java have a little difference. The original method does not have parameters, but now there are two parameters.
Both of these are any parameters that must be in any local approach.
The first parameter is JNIENV *, which is used to connect to the parameters and objects of the local method passing from the Java application. The second parameter is a Jobject, which pointing to the current object itself, you can also understand it as the THIS variable in Java. For a local instance method, such as the DisplayHelloWorld method in this example, the Jobject parameter is a reference to an object current instance. For local methods, this parameter is a reference to a method class. These two parameters don't need to be used in this example.
Another point, you can find the name of the method and the inconsistency in the Java file, this method is constructed by the following parts:
Java_ [包 名 ] class name _JAVA method name
4) Write a C file that implements a local method
// This example is HelloWorldimp.c
#include
#include "HelloWorld.h"
#include
Java_helloworld_displayhelloworld (Jnienv * Env, Jobject Obj)
{
Printf ("Hello World! / N"); // This example only outputs a hello world!
Return;
}
5) Establish a dynamic connection library
Use the following statement below Windows:
Cl -id: /jdk1.3.1/include -i d: /jdk1.3.1/include/win32 - ld helloworldimp.c -fehello.dll
There are several parts here. D: /jdk1.3.1 is the path to the local Java HOME. There is several .h files that generate dynamic connection libraries, including jni.h (including include this file in all C files for all implementation Native methods), and so on.
Place the generated .dll file in the directory where the environment variable Path can find. Run the command now:
Java HelloWorld
It will be seen as follows:
Hello World!
This is the simplest JNI usage method.