Original article: Java calls C (implementing Java local methods in Linux)
1. Declare local methods in the Java class and use the native keyword
- Public class mynative {
- Public void showparms (string S, int I, Boolean B ){
- Showparms0 (S, I, B );
- }
- Public int hypotenuse (int A, int B ){
- Return hypotenuse0 (A, B );
- }
- Public void setarray (Boolean [] BA ){
- For (INT I = 0; I <ba. length; I ++)
- Ba [I] = true;
- Setarray0 (BA );
- }
- Public void showstrings (string [] SA ){
- Showstrings0 (SA );
- }
- Public String [] getstrings (){
- Return getstrings0 ();
- }
- Private native void showparms0 (string S, int I, Boolean B );
- Private native int hypotenuse0 (int A, int B );
- Private native void setarray0 (Boolean [] BA );
- Private native void showstrings0 (string [] SA );
- Private native string [] getstrings0 ();
- Static {
- System. loadlibrary ("mynative ");
- }
- Public static void main (string [] ARGs ){
- Mynative OBJ = new mynative ();
- OBJ. showparms ("hello", 23, true );
- OBJ. showparms ("world", 34, false );
- System. Out. println (obj. hypotenuse (3, 4 ));
- System. Out. println (obj. hypotenuse (9, 12 ));
- Boolean [] BA = new Boolean [5];
- OBJ. setarray (BA );
- For (INT I = 0; I <ba. length; I ++)
- System. Out. println (BA [I]);
- String [] SA = new string [] {"hello,", "World! "," JNI "," is "," Fun ."};
- OBJ. showstrings (SA );
- OBJ. showstrings (obj. getstrings ());
- }
- }
Loadlibrary
public static void loadLibrary(String libname)
-
Loads the system library specified by
libname
Argument. The manner in which a library name is mapped to the actual system library is system dependent.
The callSystem.loadLibrary(name)
Is required tively equivalent to the call
Runtime.getRuntime().loadLibrary(name)
-
-
Parameters:
-
libname
-The name of the library.
-
Throws:
-
SecurityException
-If a security manager exists and its
checkLink
Method doesn' t allow loading of the specified dynamic library
-
UnsatisfiedLinkError
-If the library does not exist.
-
NullPointerException
-If
libname
Is
null
-
See also:
-
Runtime.loadLibrary(java.lang.String)
,
SecurityManager.checkLink(java.lang.String)
Ii. Generate. Class and. H files
- Javac mynative. Class
- Javah-JNI mynative
The generated. h file is as follows:
- /* Do not edit this file-it is machine generated */
- # Include <JNI. h>
- /* Header for class mynative */
- # Ifndef _ included_mynative
- # DEFINE _ included_mynative
- # Ifdef _ cplusplus
- Extern "C "{
- # Endif
- /*
- * Class: mynative
- * Method: getstrings0
- * Signature: () [ljava/lang/string;
- */
- Jniexport jobjectarray jnicall java_mynative_getstrings0
- (Jnienv *, jobject );
- /*
- * Class: mynative
- * Method: showstrings0
- * Signature: ([ljava/lang/string;) V
- */
- Jniexport void jnicall java_mynative_showstrings0
- (Jnienv *, jobject, jobjectarray );
- /*
- * Class: mynative
- * Method: showparms0
- * Signature: (ljava/lang/string; IZ) V
- */
- Jniexport void jnicall java_mynative_showparms0
- (Jnienv *, jobject, jstring, jint, jboolean );
- /*
- * Class: mynative
- * Method: hypotenuse0
- * Signature: (ii) I
- */
- Jniexport jint jnicall java_mynative_hypotenuse0
- (Jnienv *, jobject, jint, jint );
- /*
- * Class: mynative
- * Method: setarray0
- * Signature: ([Z) V
- */
- Jniexport void jnicall java_mynative_setarray0
- (Jnienv *, jobject, jbooleanarray );
- # Ifdef _ cplusplus
- }
- # Endif
- # Endif
3. Implement the methods declared in. h file in C Language
Include the generated. h file in the header file and use the C language to implement these local methods. The Code is as follows:
- # Include <stdio. h>
- # Include <math. h>
- # Include "mynative. H"
- Jniexport void jnicall java_mynative_showparms0
- (Jnienv * ENV, jobject OBJ, jstring S, jint I, jboolean B)
- {
- Const char * szstr = (* env)-> getstringutfchars (ENV, S, 0 );
- Printf ("string = [% s]/n", szstr );
- Printf ("Int = % d/N", I );
- Printf ("Boolean = % s/n", (B = jni_true? "True": "false "));
- (* Env)-> releasestringutfchars (ENV, S, szstr );
- }
- Jniexport jint jnicall java_mynative_hypotenuse0
- (Jnienv * ENV, jobject OBJ, jint A, jint B)
- {
- Int RTN = (INT) SQRT (double) (A * A) + (B * B )));
- Return (jint) RTN;
- }
- Jniexport void jnicall java_mynative_setarray0
- (Jnienv * ENV, jclass CLs, jbooleanarray BA)
- {
- Jboolean * PBA = (* env)-> getbooleanarrayelements (ENV, Ba, 0 );
- Jsize Len = (* env)-> getarraylength (ENV, BA );
- Int I = 0;
- // Change the even array element
- For (I = 0; I <Len; I + = 2)
- PBA [I] = jni_false;
- (* Env)-> releasebooleanarrayelements (ENV, Ba, PBA, 0 );
- }
- Jniexport void jnicall java_mynative_showstrings0
- (Jnienv * ENV, jclass CLs, jobjectarray SA)
- {
- Int Len = (* env)-> getarraylength (ENV, SA );
- Int I = 0;
- For (I = 0; I <Len; I ++)
- {
- Jobject OBJ = (* env)-> getobjectarrayelement (ENV, SA, I );
- Jstring STR = (jstring) OBJ;
- Const char * szstr = (* env)-> getstringutfchars (ENV, STR, 0 );
- Printf ("% s", szstr );
- (* Env)-> releasestringutfchars (ENV, STR, szstr );
- }
- Printf ("/N ");
- }
- Jniexport jobjectarray jnicall java_mynative_getstrings0
- (Jnienv * ENV, jclass CLs)
- {
- Jstring STR;
- Jobjectarray ARGs = 0;
- Jsize Len = 5;
- Char * Sa [] = {"hello,", "World! "," JNI "," is "," fun "};
- Int I = 0;
- ARGs = (* env)-> newobjectarray (ENV, Len, (* env)-> findclass (ENV, "Java/lang/string"), 0 );
- For (I = 0; I <Len; I ++)
- {
- STR = (* env)-> newstringutf (ENV, sa [I]);
- (* Env)-> setobjectarrayelement (ENV, argS, I, STR );
- }
- Return ARGs;
- }
4. compile C language files
Write the MAKEFILE file, compile the C code you just wrote, and generate the. So file. in Linux (Windows, DLL files)
- Libmynative. So: mynative. O makefile
- Gcc-wall-rdynamic-shared-O libmynative. So mynative. o
- Mynative. O: mynative. c mynative. h
- Gcc-wall-C mynative. C-I./-I/sandbox/java2s/jdk1.6.0 _ 16/include-I/sandbox/java2s/jdk1.6.0 _ 16/include/Linux
- CL:
- Rm-RF *. O *. So
"/Sandbox/java2s/jdk1.6.0 _ 16" is the JDK installation directory.
"Libmynative. So" is in the Java class
Static {
System. loadlibrary ("mynative ");
}
Parameters + Lib in static methods
5. Configure Environment Variables
Modify the. bash_profile file and configure the environment variables. Because the generated. So file is in the current directory
The. bash_profile file is set as follows:
Export LD_LIBRARY_PATH = ./
6. Execute the target file
Run the Java mynative command. The result is as follows:
In Linux, Java local method (Java calls C) is implemented.
References:
Http://www.ibm.com/developerworks/cn/java/jnimthds/index.html
Java is used in combination with C and C ++, Java calls C language, Java calls C ++ language, Java JNI implementation, and Java local call method.
From: http://blog.csdn.net/kangkanglou/archive/2010/08/13/5807787.aspx
Author: kangkang_kevin