Java调用C++可以通过JNI声明native方法进行调用,主要步骤有:
1、声明类及相关nativ方法,加载要调用的C++库
package bupt.cist.nlp.ner; public class ChineseNER { static{ System.loadLibrary("CIST_NER"); } public ChineseNER(){ } public native void say(String msg); public native String process(String line); } |
2、到源码目录下,通过命令javah -jni bupt.cist.nlp.ner.ChineseNER编译类,生成.h头文件
/* DO NOT EDIT THIS FILE - it is machine generated */ #include "jni.h" /* Header for class bupt_cist_nlp_ner_ChineseNER */ #ifndef _Included_bupt_cist_nlp_ner_ChineseNER #define _Included_bupt_cist_nlp_ner_ChineseNER #ifdef __cplusplus extern "C" { #endif /* * Class: bupt_cist_nlp_ner_ChineseNER * Method: say * Signature: (Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_bupt_cist_nlp_ner_ChineseNER_say (JNIEnv *, jobject, jstring); /* * Class: bupt_cist_nlp_ner_ChineseNER * Method: process * Signature: (Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_bupt_cist_nlp_ner_ChineseNER_process (JNIEnv *, jobject, jstring); #ifdef __cplusplus } #endif #endif |