10, JAVA through JNI call local C language method
JAVA JNI calls through local C language method
JAVA of its cross-platform by the popular, but because it is the purpose of the cross-platform, making it the local machine and the various internal linkages become little restrain its functions. JAVA resolved on the local operation is a method of JNI.
JAVA JNI calls through the local approach, and the local method is stored in the form of the document (in the WINDOWS platform is the form of DLL files in the UNIX machine is SO paper form). By calling the local paper the internal method, JAVA can be achieved and the close ties between the local machine, called system-level interfaces and methods.
A brief introduction and application are as follows:
1, JAVA required in the work to be done
In the JAVA program, the first class required by the statement of the name calling, as follows:
(Static
System.loadLibrary ( "goodluck");
)
Here, the expansion of the names do not have to write out what is SO DLL or by the system of judging for themselves.
Also need to be called on the local methods statement, keyword for the native. And require only a statement, without the need for concrete realization. As follows:
Public native static void set (int i);
Public native static int get ();
JAVA then compile the files, generating CLASS, reuse JAVAH orders, JNI will generate C / C + + header files.
For example, procedures testdll.java, says:
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 ());
)
)
Use javac testdll.java compile it will generate testdll.class.
Javah testdll reuse will be generated in the current directory testdll.h file needs to be C / C + + Applications to generate the necessary documents.
2, C / C + + for the work to be done
They have generated. H header files, and C / C + + need to do is to bring it all the way to achieve concrete. Then compiled into library files can be connected. Then copy files to the JAVA program can be found below, we can call with JAVA C / C + + achieved by the function.
Connect example. We turn first to testdll.h the contents of the documents:
/ * 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 the concrete realization of the time, we care only about two prototype
JNIEXPORT jint JNICALL Java_testdll_get (JNIEnv *, jclass);
And
JNIEXPORT void JNICALL Java_testdll_set (JNIEnv *, jclass, jint);
Here are JNI JNICALL JNIEXPORT and keywords that this function is to be JNI calls. JNI and jint is JAVA as an intermediary to the int type of communication with the local int type, we can turn a blind eye to use as int. JAVA_ function is the name of java procedures coupled with the package path coupled with the function of composition. Parameters, we need only interested in the JAVA program parameters, as we jclass JNIEnv * and there is no need to touch it in general.
Good, the following paper we use testdll.cpp concrete realization of these two functions:
# Include "testdll.h"
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;
)
Compiler connected into the document, in this case is under WINDOWS do, and DLL files are generated. And the name and JAVA need to call the same, here is goodluck.dll.
Testdll.class goodluck.dll copied to the directory, java testdll run it, the results can be observed.
Tags: java jni, java language, java method, java programming language, the method






