用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java调用dll或so动态链接库 System.loadLibrary()

2013-05-09 作者: 神马举报

[java]代码库

public class Sample1 {
    public native int intMethod(int n);
    public native boolean booleanMethod(boolean bool);
    public native String stringMethod(String text);
    public native int intArrayMethod(int[] intArray);

    public static void main(String[] args) {
        System.loadLibrary("Sample1");
        Sample1 sample = new Sample1();
        int square = sample.intMethod(5);
        boolean bool = sample.booleanMethod(true);
        String text = sample.stringMethod("Java");
        int sum = sample.intArrayMethod(new int[] {1, 2, 3, 4, 5, 8, 13});

        System.out.println("intMethod: " + square);
        System.out.println("booleanMethod: " + bool);
        System.out.println("stringMethod: " + text);
        System.out.println("intArrayMethod: " + sum);
    }
}




//c语言版
---------------------- -
#include "Sample1.h"
#include <string.h>

JNIEXPORT jint JNICALL Java_Sample1_intMethod
(JNIEnv * env, jobject obj, jint num) {
    return num * num;
}

JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod
(JNIEnv * env, jobject obj, jboolean boolean) {
    return !boolean;
}

JNIEXPORT jstring JNICALL Java_Sample1_stringMethod
(JNIEnv * env, jobject obj, jstring string) {
    const char * str = ( * env)->GetStringUTFChars(env, string, 0);
    char cap[128];
    strcpy(cap, str);
    ( * env)->ReleaseStringUTFChars(env, string, 0);
    return ( * env)->NewStringUTF(env, strupr(cap));
}

JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod
(JNIEnv * env, jobject obj, jintArray array) {
    int i, sum = 0;
    jsize len = ( * env)->GetArrayLength(env, array);
    jint * body = ( * env)->GetIntArrayElements(env, array, 0);

    for (i = 0; i < len; ++i) {
        sum += body[i];
    }
    ( * env)->ReleaseIntArrayElements(env, array, body, 0);
    return sum;
}






//c++语言版
---------------------- -
#include "Sample1.h"
#include <string.h>

JNIEXPORT jint JNICALL Java_Sample1_intMethod
(JNIEnv * env, jobject obj, jint num) {
    return num * num;
}

JNIEXPORT jboolean JNICALL Java_Sample1_booleanMethod
(JNIEnv * env, jobject obj, jboolean boolean) {
    return !boolean;
}

JNIEXPORT jstring JNICALL Java_Sample1_stringMethod
(JNIEnv * env, jobject obj, jstring string) {
    const char * str = env->GetStringUTFChars(string, 0);
    char cap[128];
    strcpy(cap, str);
    env->ReleaseStringUTFChars(string, 0);
    return env->NewStringUTF(strupr(cap));
}

JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod
(JNIEnv * env, jobject obj, jintArray array) {
    int i, sum = 0;
    jsize len = env->GetArrayLength(array);
    jint * body = env->GetIntArrayElements(array, 0);

    for (i = 0; i < len; ++i) {
        sum += body[i];
    }
    env->ReleaseIntArrayElements(array, body, 0);
    return sum;
}



网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...