Skip to content

Commit

Permalink
add jni tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwu51 committed Jun 18, 2024
1 parent 0facf09 commit 7525bce
Show file tree
Hide file tree
Showing 11 changed files with 7,180 additions and 0 deletions.
415 changes: 415 additions & 0 deletions 24.06/JNI教程.md

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions 24.06/jni/Main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <jni.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Main.h"

JNIEXPORT void JNICALL Java_Main_native_1hello
(JNIEnv * env, jclass cls) {
printf("Hello JNI!\n");
}

JNIEXPORT jint JNICALL Java_Main_add
(JNIEnv * env, jobject instance, jint a, jint b) {
return a + b;
}

JNIEXPORT jstring JNICALL Java_Main_hi
(JNIEnv * env, jobject instance, jstring javaString) {
char* hello = "Hello,";
const char* name = (*env)->GetStringUTFChars(env, javaString, 0);
size_t len = strlen(hello) + strlen(name) + 1;
char* result = (char*)malloc(len);
strcpy(result, hello);
strcat(result, name);
(*env)->ReleaseStringUTFChars(env, javaString, name);
return (*env)->NewStringUTF(env, result);
}
37 changes: 37 additions & 0 deletions 24.06/jni/Main.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions 24.06/jni/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Main {
static {
System.load("C:/Users/sunwu/Desktop/base/gateway/notebook/24.06/jni/libMyJNI.dll");
}

public static native void native_hello();
public native int add(int a, int b);

public native String hi(String name);

public static void main(String[] args) {
native_hello();
System.out.println(new Main().add(1, 1));
System.out.println(new Main().hi("frank"));
}
}
Loading

0 comments on commit 7525bce

Please sign in to comment.