JNIサンプルコードを試しています。
(以下のソースはすべて github から入手できます: https://github.com/pilhoon/jni-test )
- サンプル.java
public クラス サンプル
{
パブリック ネイティブ 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("サンプル");
サンプル サンプル = 新しいサンプル();
int スクエア = sample.intMethod(5);
boolean bool = sample.booleanMethod(true);
文字列テキスト = sample.stringMethod("JAVA");
int sum = sample.intArrayMethod(new int[]{1,1,2,3,5,8,13});
System.out.println("intMethod: " + square);
System.out.println("booleanMethod: " + bool);
System.out.println("stringMethod: " + テキスト);
System.out.println("intArrayMethod: " + sum);
}
}
- sample.c
#include "sample.h"
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jint JNICALL Java_Sample_intMethod
(JNIEnv *env, jobject obj, jint num) {
数値 * 数値を返します。
}
JNIEXPORT jboolean JNICALL Java_Sample_booleanMethod
(JNIEnv *env, jobject obj, jboolean boolean) {
ブール値を返します。
}
JNIEXPORT jstring JNICALL Java_Sample_stringMethod
(JNIEnv *env, jobject obj, jstring 文字列) {
const char *str = (*env)->GetStringUTFChars(env, string, 0);
文字キャップ[128];
strcpy(キャップ、str);
(*env)->ReleaseStringUTFChars(env, string, str);
return (*env)->NewStringUTF(env, strupr(cap));
}
JNIEXPORT jint JNICALL Java_Sample_intArrayMethod
(JNIEnv *env, jobject obj, jintArray 配列) {
int i、合計 = 0;
jsize len = (*env)->GetArrayLength(env, array);
jint *body = (*env)->GetIntArrayElements(env, array, 0);
for (i=0; iReleaseIntArrayElements(env, array, body, 0);
合計を返します。
}
ボイドメイン(){}
#ifdef __cplusplus
}
#endif
- サンプル.h
/* このファイルは編集しないでください - これは機械で生成されたものです */
#include <jni.h>
/* Sample クラスのヘッダー */
#ifndef _Included_Sample
#define _Included_Sample
#ifdef __cplusplus
extern "C" {
#endif
/*
* クラス: サンプル
* メソッド: int メソッド
* 署名: (I)I
*/
JNIEXPORT jint JNICALL Java_Sample_intMethod
(JNIEnv *、jobject、ジント);
/*
* クラス: サンプル
* メソッド: booleanMethod
* 署名: (Z)Z
*/
JNIEXPORT jboolean JNICALL Java_Sample_booleanMethod
(JNIEnv *、jobject、jboolean);
/*
* クラス: サンプル
* メソッド: stringMethod
* 署名: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_Sample_stringMethod
(JNIEnv *, jobject, jstring);
/*
* クラス: サンプル
* メソッド: intArrayMethod
* 署名: ([I)I
*/
JNIEXPORT jint JNICALL Java_Sample_intArrayMethod
(JNIEnv *、jobject、jintArray);
#ifdef __cplusplus
}
#endif
#endif
そして、これらをCentOS 6.3でgccでコンパイルしました
prompt$ gcc -c -o sample.o -fPIC sample.c -I /usr/java/jdk1.7.0_07/include/ -I /usr/java/jdk1.7.0_07/include/linux/ prompt$ gcc -shared -o libsample.so sample.o
しかし、「Java サンプル」を実行すると、エラーが発生します。
java: シンボル検索エラー: /home/ph/tmp/jni/libsample.so: 未定義のシンボル: strupr
どうすればこれを修正できますか?