java调用c++动态链接库
1.新建java代码 HelloWorld.java
class HelloWorld {
public native void displayHelloWorld();
static {
System.loadLibrary("hello");
}
public static void main(String[] args) {
new HelloWorld()。displayHelloWorld();
}
}
操作方法
- 01
2.javac HelloWorld.java 3.javah -jni HelloWorld 生成c++头文件HelloWorld.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class HelloWorld */ #ifndef _Included_HelloWorld #define _Included_HelloWorld #ifdef __cplusplus extern "C" { #endif /* * Class: HelloWorld * Method: displayHelloWorld * Signature: ()V */ JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
- 02
4.实现c++头文件 HelloWorld.h的头文件 HelloWorld.cpp www.cdtarena.com #include "HelloWorld.h" #include <iostream> using namespace std; JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld (JNIEnv *, jobject){ printf("this is my jni hello,world\n"); cout《"hello,world!"《endl; } 5.编译生成hello.dll动态库 工具-->选项-->source control-->include files 将jdk-->include下的头文件和win32的头文件包含进去 6.运行java HelloWorld