Skip to content

Commit 5a0378b

Browse files
committed
add jni api
1 parent a5c0441 commit 5a0378b

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

Diff for: sparrow-jni/.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
target
3+
.idea
4+
lib
5+
classes
6+
logs
7+
*.jar
8+
cmake-build-debug
9+
lib

Diff for: sparrow-jni/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
set(CMAKE_C_STANDARD 99)
3+
project(HelloWorld)
4+
set(CMAKE_C_STANDARD 99)
5+
find_package(JNI REQUIRED)
6+
include_directories(${JNI_INCLUDE_DIRS})
7+
include_directories(jni)
8+
add_library(SparrowJni SHARED src/HelloWorld.c)
9+

Diff for: sparrow-jni/jni/com_sparrow_jdk_jni_HelloWorld.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <jni.h>
2+
3+
#ifndef _Included_com_sparrow_jdk_jni_HelloWorld
4+
#define _Included_com_sparrow_jdk_jni_HelloWorld
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
JNIEXPORT jstring JNICALL Java_com_sparrow_jdk_jni_HelloWorld_hi
9+
(JNIEnv *, jclass, jstring);
10+
11+
#ifdef __cplusplus
12+
}
13+
#endif
14+
#endif

Diff for: sparrow-jni/lib/libSparrowJni.dylib

32.3 KB
Binary file not shown.

Diff for: sparrow-jni/src/HelloWorld.c

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// HelloWorld.c
2+
#include "com_sparrow_jdk_jni_HelloWorld.h"
3+
#ifdef __cplusplus
4+
extern "C"
5+
{
6+
#endif
7+
8+
JNIEXPORT jstring JNICALL Java_com_sparrow_jdk_jni_HelloWorld_hi(
9+
JNIEnv *env, jclass cls, jstring j_str)
10+
{
11+
const char *c_str = NULL;
12+
c_str = (*env)->GetStringUTFChars(env, j_str, NULL);
13+
printf( " c recv :[%s]\n" , c_str);
14+
15+
if (c_str == NULL)
16+
{
17+
printf("error.\n");
18+
return NULL;
19+
}
20+
(*env)->ReleaseStringUTFChars(env, j_str, c_str);
21+
return j_str;
22+
}
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif

0 commit comments

Comments
 (0)