使用SWIG将c ++包装到java UnsatisfiedLinkError:HeaderJNI.new_Test()J

时间:2017-01-08 15:43:14

标签: java c++ swig unsatisfiedlinkerror

我一直在尝试在我的项目中使用SWIG。似乎我错过了一些东西。请帮忙。到目前为止我基于本教程做了什么: http://web.mit.edu/svn/src/swig-1.3.25/Examples/java/class/index.html是:

1.使用Visual Studio

创建标题Header.h
class Test {
public:
    Test();
    ~Test();
    void showNumber();
private:
   int number;
};

2.created swig interface

/* File : Header.i */
%module Header

%{
#include "Header.h"
%}

/* Let's just grab the original header file here */
%include "Header.h"

3.在CMD运行中:

swig -c++ -java Header.i

4.Created SwigTest3.cpp

#include "Header.h"
#include <iostream>
Test::Test() {
    number = 12;
}
Test::~Test() {
}
void Test::showNumber() {
    std::cout << "Number: " << number << std::endl;
}

5.从cpp&amp;处理64dll使用Visual Studio

的h文件

6.Created Java main.java类

public class main{
static{
    try {
    System.load("E:\\C++ projects\\SwigTest3\\SwigTest3\\SwigTest3.dll");
    } catch (UnsatisfiedLinkError e) {
    System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
    System.exit(1);
    }

}
public static void main(String argv[]){
        System.out.println("Hello");
        Test te = new Test();
    }
}

7.Running main.java给了我

Hello
Exception in thread "main" java.lang.UnsatisfiedLinkError: HeaderJNI.new_Test()J
at HeaderJNI.new_Test(Native Method)
at Test.<init>(Test.java:38)
at main.main(main.java:13)

所有文件都在同一个文件夹中。请告诉我我做错了什么。

1 个答案:

答案 0 :(得分:0)

在步骤3中,swig创建一个* _wrap.cxx文件。

在步骤5中,您只编译.cpp文件,而不是生成的.cxx文件。