将jni运行的代码移动到test里面。

This commit is contained in:
LingZhaoHui 2024-01-23 00:34:54 +08:00
parent 205978c59f
commit 3d6a565452
Signed by: zeekling
GPG Key ID: D96E4E75267CA2CC
6 changed files with 40 additions and 33 deletions

View File

@ -44,8 +44,8 @@
<version>3.1.1</version>
<executions>
<execution>
<id>uncompress</id>
<phase>install</phase>
<id>buildJin</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>

View File

@ -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());
}
}

View File

@ -0,0 +1,21 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* 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

View File

@ -1,21 +0,0 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* 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

View File

@ -1,4 +1,4 @@
#include "com_zeekling_cn_jni_JniTest.h"
#include "com_zeekling_cn_jni_JniLoader.h"
#include <stdio.h>
#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;

View File

@ -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");
}
}