(Linux下实现Java本地方法)

一、Java类中声明本地方法,使用native关键字

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 the libname argument. The manner in which a library name is mapped to the actual system library is system dependent.
The call System.loadLibrary(name) is effectively 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)

二、生成.class文件,并使用javah命令,将.class文件生成.h文件

javac MyNative.class 
javah -jni MyNative 
javac MyNative.class
javah -jni MyNative

生成的.h文件如下

/* 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 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsgzg.html