一、
编写java类文件
public class Jnative_ {
public native String tsHello(String s);
//public native void displayHelloWorld();
static {
// System.loadLibrary("hello");
System.loadLibrary("tsw");
}
public static void main(String[] args) {
//new Jnative_().displayHelloWorld();
System.out.println(new Jnative_().tsHello("终于一次性成功了~"));
}
}
cmd -
cd 到该文件目录:
javah - jni Jnative_ 生成*.h文件。
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_pnf_jni_NativeLib */
#ifndef _Included_com_pnf_jni_NativeLib
#define _Included_com_pnf_jni_NativeLib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_pnf_jni_NativeLib
* Method: sumArray
* Signature: ([I)I
*/
JNIEXPORT jint JNICALL Java_com_pnf_jni_NativeLib_sumArray
(JNIEnv *, jobject, jintArray);
#ifdef __cplusplus
}
#endif
#endif
二、
编写实现c++
#include <jni.h>
#include <iostream>
#include "com_pnf_jni_NativeLib.h"
JNIEXPORT jint JNICALL
Java_com_pnf_jni_NativeLib_sumArray
(JNIEnv * env, jobject obj, jintArray array)
{
}
vc2008 编译命令:
cl -I "C:\Program Files\Java\jdk1.5.0_19\include" -I "C:\Program Files\Java\jdk1.5.0_19\include\win32" -LD HImp.c -Fehello.dl
HImp.c :要编译的文件
Fehello.dl:输出的文件
三、运行