From 3d6a56545270d5c0d70afa05524e35794332a66d Mon Sep 17 00:00:00 2001 From: zeekling Date: Tue, 23 Jan 2024 00:34:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86jni=E8=BF=90=E8=A1=8C=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=A7=BB=E5=8A=A8=E5=88=B0test=E9=87=8C?= =?UTF-8?q?=E9=9D=A2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 ++-- .../cn/jni/{JniTest.java => JniLoader.java} | 11 +++------- .../native/com_zeekling_cn_jni_JniLoader.h | 21 +++++++++++++++++++ src/main/native/com_zeekling_cn_jni_JniTest.h | 21 ------------------- src/main/native/jniTestNative.c | 4 ++-- .../com/zeekling/cn/jni/JniLoaderTest.java | 12 +++++++++++ 6 files changed, 40 insertions(+), 33 deletions(-) rename src/main/java/com/zeekling/cn/jni/{JniTest.java => JniLoader.java} (52%) create mode 100644 src/main/native/com_zeekling_cn_jni_JniLoader.h delete mode 100644 src/main/native/com_zeekling_cn_jni_JniTest.h create mode 100644 src/test/java/com/zeekling/cn/jni/JniLoaderTest.java diff --git a/pom.xml b/pom.xml index 81324a5..c70248e 100644 --- a/pom.xml +++ b/pom.xml @@ -44,8 +44,8 @@ 3.1.1 - uncompress - install + buildJin + compile exec diff --git a/src/main/java/com/zeekling/cn/jni/JniTest.java b/src/main/java/com/zeekling/cn/jni/JniLoader.java similarity index 52% rename from src/main/java/com/zeekling/cn/jni/JniTest.java rename to src/main/java/com/zeekling/cn/jni/JniLoader.java index ef251ff..9f2d0a1 100644 --- a/src/main/java/com/zeekling/cn/jni/JniTest.java +++ b/src/main/java/com/zeekling/cn/jni/JniLoader.java @@ -1,10 +1,10 @@ package com.zeekling.cn.jni; /** - * javac -encoding utf8 -h . JniTest.java + * javac -encoding utf8 -h . JniLoader.java * gcc -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -fPIC -shared jniTestNative.c -o libJniTestNative.so */ -public class JniTest { +public class JniLoader { /** * native关键字,表明这个方法使用java以外的语言实现 @@ -14,12 +14,7 @@ public class JniTest { static { //加载jni库so - System.load(JniTest.class.getResource("/libJniTestNative.so").getPath()); - } - - public static void main(String[] args) { - //运行后,控制台打印出 hello world - new JniTest().say("hello world"); + System.load(JniLoader.class.getResource("/libJniTestNative.so").getPath()); } } diff --git a/src/main/native/com_zeekling_cn_jni_JniLoader.h b/src/main/native/com_zeekling_cn_jni_JniLoader.h new file mode 100644 index 0000000..5da1e01 --- /dev/null +++ b/src/main/native/com_zeekling_cn_jni_JniLoader.h @@ -0,0 +1,21 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_zeekling_cn_jni_JniLoader */ + +#ifndef _Included_com_zeekling_cn_jni_JniLoader +#define _Included_com_zeekling_cn_jni_JniLoader +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_zeekling_cn_jni_JniLoader + * Method: say + * Signature: (Ljava/lang/String;)V + */ +JNIEXPORT void JNICALL Java_com_zeekling_cn_jni_JniLoader_say + (JNIEnv *, jobject, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/main/native/com_zeekling_cn_jni_JniTest.h b/src/main/native/com_zeekling_cn_jni_JniTest.h deleted file mode 100644 index 69c8ce9..0000000 --- a/src/main/native/com_zeekling_cn_jni_JniTest.h +++ /dev/null @@ -1,21 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class com_zeekling_cn_jni_JniTest */ - -#ifndef _Included_com_zeekling_cn_jni_JniTest -#define _Included_com_zeekling_cn_jni_JniTest -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: com_zeekling_cn_jni_JniTest - * Method: say - * Signature: (Ljava/lang/String;)V - */ -JNIEXPORT void JNICALL Java_com_zeekling_cn_jni_JniTest_say - (JNIEnv *, jobject, jstring); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/main/native/jniTestNative.c b/src/main/native/jniTestNative.c index b09c371..e71d0ca 100644 --- a/src/main/native/jniTestNative.c +++ b/src/main/native/jniTestNative.c @@ -1,4 +1,4 @@ -#include "com_zeekling_cn_jni_JniTest.h" +#include "com_zeekling_cn_jni_JniLoader.h" #include #ifdef __cplusplus extern "C" { @@ -8,7 +8,7 @@ extern "C" { * Method: say * Signature: (Ljava/lang/String;)V */ -JNIEXPORT void JNICALL Java_com_zeekling_cn_jni_JniTest_say(JNIEnv* env, jclass cls, jstring j_str) { +JNIEXPORT void JNICALL Java_com_zeekling_cn_jni_JniLoader_say(JNIEnv* env, jclass cls, jstring j_str) { const char *c_str = NULL; char buff[128] = { 0 }; jboolean isCopy; diff --git a/src/test/java/com/zeekling/cn/jni/JniLoaderTest.java b/src/test/java/com/zeekling/cn/jni/JniLoaderTest.java new file mode 100644 index 0000000..5a4c864 --- /dev/null +++ b/src/test/java/com/zeekling/cn/jni/JniLoaderTest.java @@ -0,0 +1,12 @@ +package com.zeekling.cn.jni; + +import org.junit.Test; + +public class JniLoaderTest { + + @Test + public void testSay() { + new JniLoader().say("hello world"); + } + +}