Implement local Java method calls in Linux

Source: Internet
Author: User
/*******************MyNative.java**********************/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());}}

Run the following command to generate the mynative. h file:

Javac mynative. Java

Javah-JNI mynative

Create the mynative. c file, include the mynative. h file, and use C to implement the methods defined in mynative. H. mynative. h and mynative. C are 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 __cplusplusextern "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

# Include <stdio. h> # include <math. h> # include "mynative. H "jniexport void jnicall encode (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 encode (jnienv * ENV, jobject OBJ, jint A, jint B) {int RTN = (INT) SQRT (double) (A * A) + (B * B); Return (jint) RTN ;} jniexport void jnicall events (jnienv * ENV, jclass CLs, jbooleanarray BA) {jboolean * PBA = (* env)-> getbooleanarrayelements (ENV, Ba, 0 ); jsize Len = (* E NV)-> getarraylength (ENV, BA); int I = 0; // change the even array element for (I = 0; I <Len; I ++ = 2) PBA [I] = jni_false; (* env)-> encrypt (ENV, Ba, PBA, 0);} jniexport void jnicall encrypt (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 encode (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 ;}

Then compile mynative. c into a dynamic link library. The MAKEFILE file is as follows:

########### Note that the file name must be makefile rather than makefile, the makefile is not compiled on my own computer ################### libmynative. so: mynative. O makefilegcc-wall-rdynamic-shared-O libmynative. so mynative. O mynative. o: mynative. c mynative. hgcc-wall-C mynative. c-I. /-I/sandbox/java2s/jdk1.6.0 _ 16/include-I/sandbox/java2s/jdk1.6.0 _ 16/include/linuxcl: Rm-RF *. O *. so

Note: The above "/sandbox/java2s/JDK ..... /include .... "modification should be made based on your actual situation. You can use" find/usr-name JNI. h-print.

Then make

Modify your environment variables and add the following information:

Export LD_LIBRARY_PATH = ./

Then, Java mynative runs the JNI Java program and you can see the output ()

-----------------------)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.