java 调用c++

 1  创建Java类  添加native方法 NativeClassDemo

public class NativeClassDemo {
    static {
        //写全路径 用load不是loadLib
        System.load("NativeClassDemo.dll");
    }
    public native static void Hello();
    public static void main(String[] args) {
        Hello();
    }
}

2利用javah来生成native方法的.h c++头文件  NativeClassDemo.h

javah -jni  NativeClass

3  修改 NativeClassDemo.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class NativeClassDemo */

#ifndef _Included_NativeClassDemo
#define _Included_NativeClassDemo
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     NativeClassDemo
 * Method:    hello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_NativeClassDemo_hello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

     将  #include <jni.h> 改为 #include "jni.h"

4  拷贝NativeClassDemo.h,jni.h,jni_md.h至vs工程cpp文件目录下

      其中jni.h在jdk的include目录下,jni_md.h在jdk的include\win32目录下

 5 在Jni.cpp中实现C++方法 

#include <iostream>
#include "NativeClassDemo.h"

using namespace std;
JNIEXPORT void JNICALL Java_NativeClassDemo_hello(JNIEnv *, jobject){

	cout << "hello succcess! first demo so exciting======" << endl;
}

  6 生成dll库(64或者32位)  

     NativeClassDemo.dll

扫描二维码关注公众号,回复: 1234609 查看本文章

   7 测试运行

    参考: https://www.cnblogs.com/baokang/p/4979243.html

猜你喜欢

转载自my.oschina.net/jamescasta/blog/1636031