JNI学習ノート
4314 ワード
1.xxx.JAvaはxxxを生成する.classファイルと専用です.hファイルコマンド
2.現在のxxxを表示します.JAvaファイルのすべての署名情報
3.Java調CはStringに入り、つづりStringは戻る
4.C javaを呼び出す変数
javac - h . xxx.java
2.現在のxxxを表示します.JAvaファイルのすべての署名情報
// xxx.class
->javap -s -p xxx.class
// ,descriptor () ;V
public class com.example.cb.test.native1.FieldTest {
public int a;
descriptor: I
public static int b;
descriptor: I
public com.example.cb.test.native1.FieldTest();
descriptor: ()V
public native void testField();
descriptor: ()V
public native void testStaticField();
descriptor: ()V
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
static {};
descriptor: ()V
}
3.Java調CはStringに入り、つづりStringは戻る
//************************************java ************************************
package com.example.cb.test.native1;
public class StringTest {
static {
//
System.load("E:\\CPlae\\NativeTest\\cmake-build-debug\\libstringLib.dll");
}
public native static String sayHello(String txt);
public static void main(String[] args) {
String txt = sayHello(" ");
System.err.println(txt);
}
}
//************************************C ************************************
#include "jni.h"
#include
#include
JNIEXPORT jstring JNICALL Java_com_example_cb_test_native1_StringTest_sayHello
(JNIEnv *env, jobject jobject, jstring jstr) {
const char *c_str = NULL;
jboolean isCopy;
c_str = (*env)->GetStringUTFChars(env, jstr, &isCopy);
if (c_str == NULL)return NULL;
// // char *
// //1.malloc
// //2.
// //3.
// char buff[128] = {0};
// sprintf(buff, "Hello %s", c_str);
// string.h API
char hello[] = "Hello ";
char *p_str = strcat(hello, c_str);
//
(*env)->ReleaseStringUTFChars(env, jstr, c_str);
return (*env)->NewStringUTF(env, p_str);
}
//************************************ ************************************
Hello
Process finished with exit code 0
4.C javaを呼び出す変数
//************************************java ************************************
package com.example.cb.test.native1;
public class FieldTest {
public int a = 1;
public static int b = 2;
public native void testField();
public native void testStaticField();
static {
System.load("E:\\CPlae\\NativeTest\\cmake-build-debug\\libfield.dll");
}
public static void main(String[] args) {
FieldTest fieldTest = new FieldTest();
fieldTest.testField();
fieldTest.testStaticField();
System.err.println("1->" + fieldTest.a);
System.err.println("2->" + fieldTest.b);
}
}
//************************************C ************************************
//C java private public
// static static
/*
* JAVA ,
* Class: com_example_cb_test_native1_FieldTest
* Method: testField
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_example_cb_test_native1_FieldTest_testField
(JNIEnv *env, jobject jobject) {
// ,
jclass clz = (*env)->GetObjectClass(env, jobject);
// , fieldID
jfieldID jfid = (*env)->GetFieldID(env, clz, "a", "I");
// , a
jint a = (*env)->GetIntField(env, jobject, jfid);
printf("C JAVA a=%d
", a);
(*env)->SetIntField(env, jobject, jfid, a + 100);
}
/*
* JAVA ,
* Class: com_example_cb_test_native1_FieldTest
* Method: testStaticField
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_example_cb_test_native1_FieldTest_testStaticField
(JNIEnv *env, jobject jobject) {
jclass clz = (*env)->GetObjectClass(env, jobject);
jfieldID fid = (*env)->GetStaticFieldID(env, clz, "b", "I");
jint b = (*env)->GetStaticIntField(env, clz, fid);
printf("C b=%d
", b);
(*env)->SetStaticIntField(env, clz, fid, b + 66);
}
//************************************ ************************************
1->101
2->101
C JAVA a=1
C b=2
Process finished with exit code 0