Created
June 5, 2020 07:39
-
-
Save fantasticmao/f6147a60f1b35b4442ca29e73b6627f8 to your computer and use it in GitHub Desktop.
Java JNI 简单实践
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <jni.h> | |
#include "cn_fantasticmao_jni_SumNative.h" | |
/* | |
* Class: cn_fantasticmao_jni_SumNative | |
* Method: sum | |
* Signature: (II)I | |
*/ | |
JNIEXPORT jint JNICALL Java_cn_fantasticmao_jni_SumNative_sum | |
(JNIEnv *env, jobject obj, jint a, jint b) | |
{ | |
return a + b; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* DO NOT EDIT THIS FILE - it is machine generated */ | |
#include <jni.h> | |
/* Header for class cn_fantasticmao_jni_SumNative */ | |
#ifndef _Included_cn_fantasticmao_jni_SumNative | |
#define _Included_cn_fantasticmao_jni_SumNative | |
#ifdef __cplusplus | |
extern "C" { | |
#endif | |
/* | |
* Class: cn_fantasticmao_jni_SumNative | |
* Method: sum | |
* Signature: (II)I | |
*/ | |
JNIEXPORT jint JNICALL Java_cn_fantasticmao_jni_SumNative_sum | |
(JNIEnv *, jobject, jint, jint); | |
#ifdef __cplusplus | |
} | |
#endif | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.fantasticmao.jni; | |
public class SumNative { | |
static { | |
System.loadLibrary("sum"); | |
} | |
public native int sum(int a, int b); | |
public static void main(String[] args) { | |
int value = new SumNative().sum(1, 2); | |
System.out.printf("1 + 2 = %d%n", value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment