JNI学习笔记,含VS配置x64平台的问题
1、来个java程序,本地方法前加native关键字
public class HelloWorld_20111226 {public static native void printHelloWorld();public static native int add(int a, int b);static {System.loadLibrary("TestJNI_201112");}public static void main(String[] args) {printHelloWorld();int sum = add(1, 2);System.out.println(sum);}}
命令行下 javah 类名(在.class的根路径下 报名.类名,无需后缀)
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class HelloWorld_20111226 */#ifndef _Included_HelloWorld_20111226#define _Included_HelloWorld_20111226#ifdef __cplusplusextern "C" {#endif/* * Class: HelloWorld_20111226 * Method: printHelloWorld * Signature: ()V */JNIEXPORT void JNICALL Java_HelloWorld_120111226_printHelloWorld (JNIEnv *, jclass);/* * Class: HelloWorld_20111226 * Method: add * Signature: (II)I */JNIEXPORT jint JNICALL Java_HelloWorld_120111226_add (JNIEnv *, jclass, jint, jint);#ifdef __cplusplus}#endif#endif
#include "HelloWorld_20111226.h"#include <iostream>JNIEXPORT void JNICALL Java_HelloWorld_120111226_printHelloWorld( JNIEnv *, jclass ){std::cout<<"Hello world"<<std::endl;}JNIEXPORT jint JNICALL Java_HelloWorld_120111226_add( JNIEnv *, jclass, jint a, jint b){return a+b;}
//main.def文件如下:LIBRARY "TestJNI_201112"EXPORTSJava_HelloWorld_120111226_printHelloWorldJava_HelloWorld_120111226_addJava_LC_TestJNI_printIntJava_LC_TestJNI_printIntArray
#include "Maxflow/LCEnergyMinimize_Graph.h"#include "GraphMinimization_GraphCut.h"typedef LCEnergyMinimize_Graph<jint,jlong> GraphIntLong;GraphIntLong* g;JNIEXPORT void JNICALL Java_GraphMinimization_GraphCut_generateGraph( JNIEnv *, jobject ){if(g!=NULL)delete g;g=new GraphIntLong();}/* * Class: GraphMinimization_GraphCut * Method: releaseGraph * Signature: ()V */JNIEXPORT void JNICALL Java_GraphMinimization_GraphCut_releaseGraph (JNIEnv *, jobject){ if(g!=NULL)delete g; g=NULL;}/* * Class: GraphMinimization_GraphCut * Method: addVariable * Signature: (I)I */JNIEXPORT jint JNICALL Java_GraphMinimization_GraphCut_addVariable (JNIEnv *, jobject, jint num){ return g->addVariable(num);}
Stack: [0x0000000001ca0000,0x0000000001da0000], sp=0x0000000001d9f750, free space=1021kNative frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)C [MaxflowJNIInterface.dll+0x1a74] Graph<long,long,__int64>::add_edge+0x74Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)J GraphMinimization.GraphCut.addTerm2(IIIIII)VJ app.StereoCorrespodenceUsingGraphCut.generateStereoCorrespodenceGraph(Llc/util/image/IntPair;)LGraphMinimization/GraphCut;j app.StereoCorrespodenceUsingGraphCut.calculate()V+52j app.StereoCorrespodenceUsingGraphCut.main([Ljava/lang/String;)V+62v ~StubRoutines::call_stub