java native 笔记

来源:互联网 发布:黄金时时彩软件下载 编辑:程序博客网 时间:2024/05/17 01:59

java生成.h文件命令 javah -jni CeNative

编译C++需要在%JAVA_HOME%/include/下找到 jni.h,在%JAVA_HOME%/include/win32/下找到jni_md.h 和 jawt_md.h,复制到“Visual Studio目录/VC/include/”下

CeNative.java

public class CeNative {public native void showHello();public native void show(String str);public native String returnStr();static{System.loadLibrary("JavaNative");}public static void main(String[] args){CeNative ce = new CeNative();ce.showHello();ce.show("aakkk\n");System.out.println(ce.returnStr());}}


CeNative.h

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class CeNative */#ifndef _Included_CeNative#define _Included_CeNative#ifdef __cplusplusextern "C" {#endif/* * Class:     CeNative * Method:    showHello * Signature: ()V */JNIEXPORT void JNICALL Java_CeNative_showHello  (JNIEnv *, jobject);/* * Class:     CeNative * Method:    show * Signature: (Ljava/lang/String;)V */JNIEXPORT void JNICALL Java_CeNative_show  (JNIEnv *, jobject, jstring);/* * Class:     CeNative * Method:    returnStr * Signature: ()Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_CeNative_returnStr  (JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif

JavaNative.cpp

// JavaNative.cpp : 定义 DLL 应用程序的导出函数。//#include "stdafx.h"#include "CeNative.h"JNIEXPORT void JNICALL Java_CeNative_showHello(JNIEnv *, jobject){const char *msg; msg = "Hello Native\n"; printf(msg);}JNIEXPORT void JNICALL Java_CeNative_show  (JNIEnv * env, jobject, jstring param){const char *msg = env->GetStringUTFChars(param, NULL);printf(msg);env->ReleaseStringUTFChars(param, msg);}JNIEXPORT jstring JNICALL Java_CeNative_returnStr  (JNIEnv * env, jobject){return env->NewStringUTF("return");}

生成DLL放在Java项目根目录就可以了