f980548079 2014-07-21 12:26
浏览 363
已采纳

java 调用第三方 动态库

[code="java"]
public class TestJava{
{
System.loadLibrary("test");
}
public native int add(int a,int b);

    public static void main(String argvs[])
    {

            TestJava ts=new TestJava();
            System.out.println(ts.add(4,5));
    }

}

[/code]
c++ test.h
[code="c++"]
#ifndef TEST_H
#define TEST_H
int add (int a,int b)
#endif
[/code]

test.cpp
[code="c++"]

#include "test.h"

int add (int a,int b)
{
return a+b;
}

[/code]
编译libtest.so
g++ -fPIC -shared test.cpp -olibtest.so

编译运行java
javac TestJava.java
java TestJava

报错如下:

Exception in thread "main" java.lang.UnsatisfiedLinkError: TestJava.add(II)I
at TestJava.add(Native Method)
at TestJava.main(TestJava.java:11)
这个什么原因,请指教。

  • 写回答

4条回答 默认 最新

  • hxfeng_helloworld 2014-07-21 21:32
    关注

    用法错误,根据JNI 官方提供的文档,使用JNI调用第三方动态库需要 用javah编译生成头文件,根据这个头文件编译生成动态库才能调用成功,如果单纯调用第三方库的话推荐使用JNA 或者jnative这些都是可以的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?