java代码
package com.easou.abo.jnitest;
public class Demo {
public int number = 10;
public native void test();
public static void main(String[] args) {
System.loadLibrary("testJni");
Demo dm = new Demo();
dm.test();
}
public double max(double a,double b) {
return (a>b)?a:b;
}
}
C代码
#include "com_easou_abo_jnitest_Demo.h"
#include <stdio.h>
#include<iostream>
using namespace std;
JNIEXPORT void JNICALL
Java_com_easou_abo_jnitest_Demo_test(JNIEnv * env, jobject obj){
jclass hello_clazz = env->GetObjectClass(obj);//获得java中的类
jmethodID jmax = env->GetMethodID(hello_clazz,"max","(DD)D");
jdouble maxValue = env->CallDoubleMethod(obj,jmax,5.1,5.2);
cout << maxValue << endl;
}
输出为5.2