一.開發一個dll動态庫
1.cpp
#include "testJNI.h"
#include <windows.h>
#include <iostream>
//Java_包名(com_xxx_xxx_)_類名_函數名
//Java_com_lzb_test_TestJNI_test
JNIEXPORT void JNICALL Java_TestJNI_test(JNIEnv*, jobject) {
using namespace std;
cout << "test";
}
2.引入Java的jni頭檔案

這兩個頭檔案都要拷到vsstudio裡
3.寫頭檔案
/* 隻要引入一個jni.h就行,他裡面引入了jni_md.h */
#include "jni.h"
#ifndef _Included_TestJNI
#define _Included_TestJNI
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java_TestJNI_test
(JNIEnv*, jobject);
#ifdef __cplusplus
}
#endif
#endif
4.打包
二.寫Java
//注意不寫package
public class TestJNI {
{
/**
* 加載dll位址
*/
System.load("C:\\Users\\dell\\Desktop\\jni\\testJNI.dll");
}
/**
* 就這個natice關鍵字.标記了這個接口
*/
public native void test();
public static void main(String[] args) {
new TestJNI().test();
System.out.println("12356456");
}
}
3.編譯
1.打開cmd
2.執行到檔案目錄下執行javac,進行編譯
這裡因為報了一個錯
是以改了一下編碼
javac -encoding UTF-8 TestJNI.java