A simple case of invoking C + + programs in a Java Web project
This is a DLL program that invokes C + + through JNI technology through Java calls
Before you start, learn what some JNI is. :
JNI is the abbreviation for Java Native interface, which is called Java Local. Starting with Java1.1, the Java Native Interface (JNI) standard became part of the Java platform,
It allows Java code to interact with code written in other languages. JNI was first designed for locally compiled languages, especially C and C + +, but it doesn't prevent you from using other languages,
This is OK as long as the calling convention is supported. Using Java to interact with locally compiled code typically loses platform portability.
However, in some cases this is acceptable and even necessary, such as using some old libraries, interacting with hardware, operating systems, or improving program performance.
The JNI standard ensures that local code can work at least under any Java Virtual machine implementation.
The case is as follows:
1, first create a new Java Web project for PROEJCT, and then write a Java class, because you want to use JNI to invoke C + + programs, so the method directory in this class needs to be decorated with native and only method declarations, not implemented,
As follows:
Package Com.coffee.common;
public class Facerecognition
{public
native static string Facerecognition (string faceimgpath);
}
2. Use the Javah command to compile the header file in eclipse;
In Elipse click "Run"--> "External Tools"--> "External tools configurations. 】
The test pops up a window and selects "program" right click New, as shown in figure:
This allows us to compile with the doc environment in Eclipse, like the figure, enter the command in the red box, and then return to the Javah compile:
3, create a new C + + project, using the VS tool, as shown in the Picture: (or you can use other better way)
4, add the necessary header file in the new C + + project
Two h header files in JDK: jni.h jni_md.h Two files directory is located under the installed JDK directory under include and Include/win32
Then we compile the Java header file before joining:
Copy the three files to the new C + + project and add the header folder, where we can create a new C + + source code file to
Write our logical code: the structure shown in the figure
5, preparation of C + + code before the work
Open a header file that is compiled with Javah to change #include <jni.h> to #include "jni.h"
and copy the method in the H file to FaceRecognition.cpp, as shown in the figure:
Then build the solution for the C + + project, and then locate the Face_recognition.dll file in the project's debug directory and copy it to
The bin directory of the Tomcat installation directory.
Of course, before using the native method, you need to load the DLL file in, at which point we can add the Load method in the Facerecognition class
Package Com.coffee.common;
public class Facerecognition
{
static{
system.loadlibrary ("face_recognition");
}
Public native static string Facerecognition (string faceimgpath);
}
6. Finally, the method with native is invoked in the business logic to start Tomcat so that we can make an access call.
public class loginserver{
private facerecognition fr;
public void Login () {
FR = new Facerecognition ();
Fr.facerecognition ("c:\\ddd");
}