From f8853815feb3df652b5c97b1d3ddc8b0325c5f96 Mon Sep 17 00:00:00 2001 From: zeekling Date: Sun, 31 Dec 2023 14:52:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0jni=20=E6=A0=B7=E4=BE=8B?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + pom.xml | 85 ++++++++++++------- .../java/com/zeekling/cn/jni/JniTest.java | 25 ++++++ src/main/native/build.sh | 13 +++ src/main/native/com_zeekling_cn_jni_JniTest.h | 21 +++++ src/main/native/jniTestNative.c | 27 ++++++ 6 files changed, 142 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/zeekling/cn/jni/JniTest.java create mode 100755 src/main/native/build.sh create mode 100644 src/main/native/com_zeekling_cn_jni_JniTest.h create mode 100644 src/main/native/jniTestNative.c diff --git a/.gitignore b/.gitignore index 4c2add1..b85849e 100644 --- a/.gitignore +++ b/.gitignore @@ -47,4 +47,5 @@ replay_pid* .idea target +*.o diff --git a/pom.xml b/pom.xml index e1ff747..81324a5 100644 --- a/pom.xml +++ b/pom.xml @@ -1,39 +1,64 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.zeekling.cn - test - 1.0-SNAPSHOT - jar + com.zeekling.cn + test + 1.0-SNAPSHOT + jar - java_test + java_test - - UTF-8 - 1.8 - 1.8 - + + UTF-8 + 1.8 + 1.8 + - + - - org.rocksdb - rocksdbjni - 6.20.3 - + + org.rocksdb + rocksdbjni + 6.20.3 + - - org.apache.hadoop - hadoop-common - 3.3.6 - + + org.apache.hadoop + hadoop-common + 3.3.6 + - - junit - junit - 4.13.2 - test - - + + junit + junit + 4.13.2 + test + + + + + + exec-maven-plugin + org.codehaus.mojo + 3.1.1 + + + uncompress + install + + exec + + + + + ${basedir}/src/main/native/build.sh + + + + maven-compiler-plugin + 3.8.1 + + + diff --git a/src/main/java/com/zeekling/cn/jni/JniTest.java b/src/main/java/com/zeekling/cn/jni/JniTest.java new file mode 100644 index 0000000..ef251ff --- /dev/null +++ b/src/main/java/com/zeekling/cn/jni/JniTest.java @@ -0,0 +1,25 @@ +package com.zeekling.cn.jni; + +/** + * javac -encoding utf8 -h . JniTest.java + * gcc -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -fPIC -shared jniTestNative.c -o libJniTestNative.so + */ +public class JniTest { + + /** + * native关键字,表明这个方法使用java以外的语言实现 + */ + public native void say(String something); + + + static { + //加载jni库so + System.load(JniTest.class.getResource("/libJniTestNative.so").getPath()); + } + + public static void main(String[] args) { + //运行后,控制台打印出 hello world + new JniTest().say("hello world"); + } + +} diff --git a/src/main/native/build.sh b/src/main/native/build.sh new file mode 100755 index 0000000..ed303da --- /dev/null +++ b/src/main/native/build.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +if [ -z "$JAVA_HOME" ]; then + JAVA_HOME="/opt/bisheng-jdk-17.0.3" +fi +echo "$JAVA_HOME" +basedir=$(pwd) +cd "$basedir/src/main/native/" +gcc -I$JAVA_HOME/include -I$JAVA_HOME/include/linux -fPIC -shared jniTestNative.c -o libJniTestNative.so + +echo "$(pwd)" + +mv libJniTestNative.so ${basedir}/target/classes/ \ No newline at end of file diff --git a/src/main/native/com_zeekling_cn_jni_JniTest.h b/src/main/native/com_zeekling_cn_jni_JniTest.h new file mode 100644 index 0000000..69c8ce9 --- /dev/null +++ b/src/main/native/com_zeekling_cn_jni_JniTest.h @@ -0,0 +1,21 @@ +/* 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 new file mode 100644 index 0000000..12368dc --- /dev/null +++ b/src/main/native/jniTestNative.c @@ -0,0 +1,27 @@ +#include "com_zeekling_cn_jni_JniTest.h" +#include +#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* env, jclass cls, jstring j_str) { + const char *c_str = NULL; + char buff[128] = { 0 }; + c_str = (*env)->GetStringUTFChars(env, j_str, NULL); + if (c_str == NULL) + { + printf("out of memory.\n"); + return NULL; + } + (*env)->ReleaseStringUTFChars(env, j_str, c_str); + printf("Hello,I'm C++,the Java Str is:%s\n", c_str); + sprintf(buff, "hello %s", c_str); + return (*env)->NewStringUTF(env, buff); +} +#ifdef __cplusplus +} +#endif \ No newline at end of file -- 2.45.2