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