JNI = Java Native Interface, which provides interfaces for Java applications to call local methods.
The standard Java class library may not support the platform-dependent features needed by your application.
You may already have a library or application written in another programming language and you wish to make it accessible to Java applications.
You may want to implement a small portion of time-critical code in a lower-level programming language, such as assembly, and then have your Java application call these functions
Steps:
1. Compile a Java package JNI class with native Declaration;
Public class helloworld ...{
Static ...{
System. loadlibrary ("hellodll ");
}
Public native void disphelloworld ();
/***//**
* @ Param ARGs
*/
Public static void main (string [] ARGs )...{
// Todo auto-generated method stub
// System. Out. println ("ABC ");
New helloworld (). disphelloworld ();
}
}
2. If there is no error after saving, eclipse will compile and generate JNI/helloworld. Class
3. Assume that all the classes generated by the project are in the classes folder (the folder name is related to the personal eclipse settings, or the project root directory ), then, the console CD to classes uses the javah command to generate an H file, xxx/classes> javah-JNI. helloworld (package should not be missed). The jni_helloworld.h file generated is as follows/** // * Do not edit this file-it is machine generated */
# Include <JNI. h>
/** // * Header for class jni_helloworld */
# Ifndef _ included_jni_helloworld
# DEFINE _ included_jni_helloworld
# Ifdef _ cplusplus
Extern "C "...{
# Endif
/**//*
* Class: jni_helloworld
* Method: disphelloworld
* Signature: () V
*/
Jniexport void jnicall java_jni_helloworld_disphelloworld
(Jnienv *, jobject );
# Ifdef _ cplusplus
}
# Endif
# Endif
4. Write CPP to implement the functions in jni_helloworld.h. Pay attention to the function name here. The function name in CPP must be consistent with the function name in the H file. If you copy some examples from the Internet, the function name is inconsistent with the name of your example. Although the DLL can be correctly compiled and generated later, an error occurs when you finally run the Java program Link. lang. unsatisfiedlinkerror is abnormal and I am wasting a long time here.
The content of jni_helloworldimp.cpp is as follows: # include <JNI. h>
# Include "jni_helloworld.h"
# Include <stdio. h>
Jniexport void jnicall java_jni_helloworld_disphelloworld
(Jnienv * ENV, jobject OBJ)
...{
Printf ("Hello world! ");
Return;
}
5. Compile CPP to generate DLL. In the console,> Cl-I % java_home %/include/Win32-LD jni_helloworldimp.cpp-fehellodll. dll
In the parameters of the preceding command,-I indicates the additional Directory included in the compilation,-LD indicates that the DLL is generated, and-Fe indicates the name of the generated DLL, you can use the Cl-HELP command to view specific parameters. For details about Cl environment configuration, refer to compiling CL in the console with vs compiler.
If compilation is successful, three files, hellodll. dll, hellodll. Lib, and hellodll. exp, will be generated, and hellodll. dll will be taken to the project root directory.
6. Run the Java program and output Hello world!