JNI C调用自己成功的详细步骤

1. 编写java测试文件:


这里附带package的,不带的很简单但是不实用


TestNative.java


package com.test.TestNative;

public class TestNative{
 
  private native int add(int x,int y);
 
  public static void main(String[] args) {
    TestNative hh = new TestNative();
    int r = hh.add(30,20);
    System.out.println("result = "+r);
  }

  static {
     System.load(System.getProperty("user.dir")+"/TestJni.dll");  
  }
}


2.  使用javac来编译此java文件,生成TestNative.class

3.  用javah -classpath . com.test.TestNative.TestNative生成h头

4.  用VC之类的IDE(在linux下你直接-shared -fPIC 出一个lib*.so一样的),把对应h的c文件写出相应逻辑,再编出对应动态库

5.  将动态库拷到com包文件夹外面,用java com.test.TestNative.TestNative来运行


其中javah -classpath . com.test.TestNative.TestNative比较重要。



PS:


在windows下面一定要编译出release不带debug的link版本,否则可能会有错误。

在DLL报错link时候,用DEPENDS.EXE这个dll依赖工具查看下,比较实用












發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章