JNI, jint type example
자바의 int 형과 같은 기본형 데이터 타입을 JNI에서 다루는 예
JNITest.java
public class JNITest
{
static{
System.loadLibrary("my_dll");
}
public native int add(int a, int b);
public static void main(String[] args)
{
JNITest test = new JNITest();
int result = test.add(3, 5);
System.out.println("C 함수 리턴값: "+result);
}
}
JNITest.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class JNITest */
#ifndef _Included_JNITest
#define _Included_JNITest
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: JNITest
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_JNITest_add
(JNIEnv *, jobject, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
my_dll.c
#include <stdio.h>
#include "JNITest.h"
JNIEXPORT jint JNICALL Java_JNITest_add
(JNIEnv *env, jobject obj, jint a, jint b) {
jint result = a+b;
printf("%d + %d = %d \n", a, b, a+b);
return result;
}