#include "com_git_MathInterface.h"
using namespace std;
JNIEXPORT jint JNICALL Java_com_git_MathInterface_add
(JNIEnv * jni, jclass clazz, jint a, jint b){
return a+b;
}
JNIEXPORT jobject JNICALL Java_com_git_MathInterface_addInteger
(JNIEnv * jni, jobject obj, jobject a, jobject b){
jclass class_Test =jni->GetObjectClass(a); //注释(1)
jfieldID id_age = (jni)->GetFieldID(class_Test, "value", "I");
jint ac = jni->GetIntField(a,id_age);
jfieldID bId = (jni)->GetFieldID(class_Test, "value", "I");
jint bc = jni->GetIntField(b,bId);
jint result = ac+bc;
jmethodID valueOf = jni->GetStaticMethodID(class_Test,"valueOf","(I)Ljava/lang/Integer;");
jobject c = jni->CallStaticObjectMethod(class_Test,valueOf,result);
return c;
}
JNIEXPORT jobject JNICALL Java_com_git_MathInterface_getOrder
(JNIEnv * jni, jobject object, jobject orderNo){
jclass longClass =jni->GetObjectClass(orderNo);
jclass orederClass =jni->FindClass("Lcom/git/Order;");
//jmethodID valueOf = jni->GetStaticMethodID(longClass,"valueOf","(J)Ljava/lang/Long;");
//jobject orderNo = jni->CallStaticObjectMethod(longClass,valueOf,orderNo);
jmethodID newOrder = jni->GetMethodID(orederClass,"<init>","()V");
jobject order = jni->NewObjectA(orederClass, newOrder,0);
jmethodID setOrderNo = jni->GetMethodID(orederClass,"setOrderNo","(Ljava/lang/Long;)V");
jni->CallObjectMethod(order,setOrderNo,orderNo);
jstring orderName = jni->NewStringUTF("订单008");
jmethodID setName = jni->GetMethodID(orederClass,"setName","(Ljava/lang/String;)V");
jni->CallObjectMethod(order,setName,orderName);
return order;
}
JNIEXPORT jobject JNICALL Java_com_git_MathInterface_checkOrder
(JNIEnv * jni, jobject object, jobject order){
return nullptr;
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
#include <iostream>
/* Header for class com_git_MathInterface */
#ifndef _Included_com_git_MathInterface
#define _Included_com_git_MathInterface
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_git_MathInterface
* Method: add
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_git_MathInterface_add
(JNIEnv *, jclass, jint a, jint b);
/*
* Class: com_git_MathInterface
* Method: addInteger
* Signature: (Ljava/lang/Integer;Ljava/lang/Integer;)Ljava/lang/Integer;
*/
JNIEXPORT jobject JNICALL Java_com_git_MathInterface_addInteger
(JNIEnv *, jobject, jobject a, jobject b);
/*
* Class: com_git_MathInterface
* Method: getOrder
* Signature: (Ljava/lang/Long;)Lcom/git/Order;
*/
JNIEXPORT jobject JNICALL Java_com_git_MathInterface_getOrder
(JNIEnv * jni, jobject object, jobject orderNo);
/*
* Class: com_git_MathInterface
* Method: checkOrder
* Signature: (Lcom/git/Order;)Ljava/lang/Boolean;
*/
JNIEXPORT jobject JNICALL Java_com_git_MathInterface_checkOrder
(JNIEnv * jni, jobject object, jobject order);
#ifdef __cplusplus
}
#endif
#endif
CONFIG -= qt
TEMPLATE = lib
DEFINES += JNI_TEST_SO_LIBRARY
CONFIG += c++11
CONFIG += dll
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
com_git_MathInterface.cpp
HEADERS += \
com_git_MathInterface.h \
jawt_md.h \
jni.h \
jni_md.h
# Default rules for deployment.
unix {
target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target
public class MathInterface {
public native static int add(int i,int j);
public native Integer addInteger(Integer i,Integer j);
public native Order getOrder(Long orderNo);
public native Boolean checkOrder(Order order);
}
/**
* @author authorZhao
* @since 2021-03-10
*/
@Data
public class Order {
private Long orderNo;
private String name;
}
public class Main {
static {
System.loadLibrary("jni_test_so");
}
public static void main(String[] args) {
/*String jni_test_so = MathInterface.class.getClassLoader().getResource("jni_test_so").getPath();
System.out.println("jni_test_so = " + jni_test_so);*/
MathInterface mathInterface = new MathInterface();
int result = MathInterface.add(8, 9);
Integer integer = mathInterface.addInteger(20, 18);
Order order = mathInterface.getOrder(18L);
System.out.println("order = " + order);
System.out.println("result = " + result);
System.out.println("integer = " + integer);
}
}
\jdk1.8\jre\bin