Java and C ++ mixed programming Java calls the DLL generated by C/C ++ (convert)
19:36:03 | category:
Programming | font size subscription
Java is favored by people for its cross-platform features. Due to its cross-platform purpose, Java has little internal connection with local machines and limits its functions. One way to solve Java local operations is JNI.
Java calls local methods through JNI, while local methods are stored in the form of library files (on Windows platforms, DLL files and so files on UNIX machines ). By calling the internal method of the local library file, Java can achieve close connection with the local machine and call system-level interface methods.
A brief introduction and application are as follows:
I. What needs to be done in Java
In a Java program, you must declare the name of the called library in the class as follows:
Static {
System. loadlibrary ("Goodluck ");
}
Here, you do not need to write the extension name of the Library. The system determines whether the extension name is DLL or so.
You also need to make a local Declaration on the method to be called. The keyword is native. In addition, you only need to declare it, instead of implementing it. As follows:
Public native static void set (int I );
Public native static int get ();
Then compile the Java program file, generate the class, and use the javah command, JNI will generate the header file of C/C ++.
For example, the content of the program testdll. Java is:
Public class testdll
{
Static
{
System. loadlibrary ("Goodluck ");
}
Public native static int get ();
Public native static void set (int I );
Public static void main (string [] ARGs)
{
Testdll test = new testdll ();
Test. Set (10 );
System. Out. println (test. Get ());
}
}
Compile it with javac testdll. Java to generate testdll. Class.
Use javah-classpath. testdll (without. Class) and-classpath
. --- Indicates finding a class in the current directory. The testdll. h file is generated in the current directory. This file needs to be called by the C/C ++ program to generate the required library file.
When writing JSP files, there are usually package names. This should be used at this time:
Javah-classpath. packagename. classname
II. C/C ++
Preparations are important:
Add the absolute paths of the JNI. h and jni_md.h files to the tools/options/directory of Vc in/JDK/include and/JDK/include/Win32.
For the generated. h header file, all C/C ++ needs to do is to implement each of its methods. Compile and connect to the database file. Copy the library file to the path of the Java program, and you can use Java to call the functions implemented by C/C ++.
Connect to the example. Let's take a look at the content of testdll. h:
/* Do not edit this file-it is machine generated */
# Include <JNI. h>
/* Header for class testdll */
# Ifndef _ included_testdll
# DEFINE _ included_testdll
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class:
Testdll
* Method:
Get
* Signature: () I
*/
Jniexport jint jnicall java_testdll_get
(Jnienv *, jclass );
/*
* Class:
Testdll
* Method:
Set
* Signature: (I) V
*/
Jniexport void jnicall java_testdll_set
(Jnienv *, jclass, jint );
# Ifdef _ cplusplus
}
# Endif
# Endif
In actual implementation, we only care about two function prototypes.
Jniexport jint jnicall java_testdll_get (jnienv *, jclass );
And
Jniexport void jnicall java_testdll_set (jnienv *, jclass, jint );
Here, both jniexport and jnicall are JNI keywords, indicating that this function is called by JNI. Jint is a type of communication between the Java int type and the local int type through JNI. We can ignore it and use it as an int. The function name is composed of Java _, the package path of the Java program, and the function name. Parameters, we only need to care about the parameters that exist in the Java program. As for jnienv * And jclass, we generally do not need to touch it.
Well, we will use the testdll. cpp file to implement these two functions:
# Include "testdll. H" // here testdll. H has prepared for generating the dynamic link library for Java calls. We do not need to write the header file that generates the dynamic link library.
Int I = 0;
Jniexport jint jnicall java_testdll_get (jnienv *, jclass)
{
Return I;
}
Jniexport void jnicall java_testdll_set (jnienv *, jclass, jint J)
{
I = J;
}
Compile and connect to the library file. In this example, it is done in windows and the DLL file is generated. And the name must be the same as that to be called in Java. Here is Goodluck. dll.
Copy Goodluck. DLL to the directory testdll. class. If your output file is in the bin directory, place the dynamic link library in the directory where the. classpath file is located. Java
Run testdll to see the result.