如何处理“java.lang.UnsatisfiedLinkError”当库路径满足时?
我正在尝试通过 JNI 导出 C++ 库函数,我有一个名为“Pylon.h”的头文件,这是源代码:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Pylon */
#ifndef _Included_Pylon
#define _Included_Pylon
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Pylon
* Method: PylonInitialize
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Pylon_PylonInitialize
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
用于调用本机方法的 C++ 代码是这样的:
#include <jni.h>
#include "testcamera.h"
#include <pylon/PylonIncludes.h>
#ifdef PYLON_WIN_BUILD
#include <pylon/PylonGUI.h>
#endif
using namespace std;
using namespace Pylon;
JNIEXPORT void JNICALL Java_testcamera_mytest(JNIEnv *, jobject) {
PylonInitialize();
}
.so 文件是用这个命令:
g++ -std=c++11 -shared -fPIC -lbxapi -lFirmwareUpdate_gcc_v3_1_Basler_pylon -lGCBase_gcc_v3_1_Basler_pylon -lGenApi_gcc_v3_1_Basler_pylon -lgxapi -lgxapi-6.3.0 -llog4cpp_gcc_v3_1_Basler_pylon -lLog_gcc_v3_1_Basler_pylon -lMathParser_gcc_v3_1_Basler_pylon -lNodeMapData_gcc_v3_1_Basler_pylon -lpylonbase-6.3.0 -lpylonbase-6.3.0 -lpylonc -lpylonc-6.3.0 -lpylon_TL_bcon -lpylon_TL_bcon-6.3.0 -lpylon_TL_camemu-6.3.0 -lpylon_TL_camemu-6.3.0 -lpylon_TL_gige -lpylon_TL_gige-6.3.0 -lpylon_TL_gtc -lpylon_TL_gtc-6.3.0 -lpylon_TL_usb-6.3.0 -lpylon_TL_usb-6.3.0 -lpylonutility -lpylonutility-6.3.0 -luxapi -luxapi-6.3.0 -lXmlParser_gcc_v3_1_Basler_pylon -L/opt/pylon/lib -I/opt/pylon/include -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux TestCamera.cpp -o libmytest.so
所有这些都是Pylon和Basler提供的各种功能的库。我的java代码是这样的:
public class testcamera {
static {
System.loadLibrary("mytest");
}
public testcamera() {}
public static void main(String[] args){
Pylon prova = new Pylon();
prova.PylonInitialize();
}
}
其中Pylon是与testcamera在同一目录中的类。然而,当我编译并运行代码时,Java 给出了这个错误:
Exception in thread "main" java.lang.UnsatisfiedLinkError: 'void Pylon.PylonInitialize()'
at Pylon.PylonInitialize(Native Method)
at testcamera.main(testcamera.java:19)
我该如何解决这个问题?我的流程正确吗?
I'm trying to export a c++ library function through JNI, I've this header file called "Pylon.h" here's the source code:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Pylon */
#ifndef _Included_Pylon
#define _Included_Pylon
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Pylon
* Method: PylonInitialize
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Pylon_PylonInitialize
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
And the c++ code used to call the native method is this:
#include <jni.h>
#include "testcamera.h"
#include <pylon/PylonIncludes.h>
#ifdef PYLON_WIN_BUILD
#include <pylon/PylonGUI.h>
#endif
using namespace std;
using namespace Pylon;
JNIEXPORT void JNICALL Java_testcamera_mytest(JNIEnv *, jobject) {
PylonInitialize();
}
The .so file is generated with this command:
g++ -std=c++11 -shared -fPIC -lbxapi -lFirmwareUpdate_gcc_v3_1_Basler_pylon -lGCBase_gcc_v3_1_Basler_pylon -lGenApi_gcc_v3_1_Basler_pylon -lgxapi -lgxapi-6.3.0 -llog4cpp_gcc_v3_1_Basler_pylon -lLog_gcc_v3_1_Basler_pylon -lMathParser_gcc_v3_1_Basler_pylon -lNodeMapData_gcc_v3_1_Basler_pylon -lpylonbase-6.3.0 -lpylonbase-6.3.0 -lpylonc -lpylonc-6.3.0 -lpylon_TL_bcon -lpylon_TL_bcon-6.3.0 -lpylon_TL_camemu-6.3.0 -lpylon_TL_camemu-6.3.0 -lpylon_TL_gige -lpylon_TL_gige-6.3.0 -lpylon_TL_gtc -lpylon_TL_gtc-6.3.0 -lpylon_TL_usb-6.3.0 -lpylon_TL_usb-6.3.0 -lpylonutility -lpylonutility-6.3.0 -luxapi -luxapi-6.3.0 -lXmlParser_gcc_v3_1_Basler_pylon -L/opt/pylon/lib -I/opt/pylon/include -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux TestCamera.cpp -o libmytest.so
All of this are the libraries for the various function provided by Pylon and Basler. My java code is like this:
public class testcamera {
static {
System.loadLibrary("mytest");
}
public testcamera() {}
public static void main(String[] args){
Pylon prova = new Pylon();
prova.PylonInitialize();
}
}
Where Pylon is a class in the same directory as testcamera. When I compile and run the code, however, Java gives me this error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: 'void Pylon.PylonInitialize()'
at Pylon.PylonInitialize(Native Method)
at testcamera.main(testcamera.java:19)
How can I solve this? Am I doing the process right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该为代码中使用的标头传递相应的 .dll 文件名。
例如。在 C++ 中,如果我添加以下头文件,
那么我的 g++ cmd 将会是,
您可以通过搜索特定 API 方法来查看 Microsoft 网站,他们可能已经提到了该 API 的要求。例如,如果您想使用“VariantInit()”,那么您需要在链接 dll (g++ cmd) 时在命令中添加“OleAut32”。
https://learn.microsoft.com/ en-us/windows/win32/api/oleauto/nf-oleauto-variantinit
You should pass the respective .dll file names for the headers that you're using in your code.
For eg. in C++ if I'm adding the below header files,
Then my g++ cmd would be,
You can check Microsoft website by searching the specific API methods, they might have mentioned the requirements for that API. For eg, if you want to use "VariantInit()" then you need to add "OleAut32" in your command while linking the dll (g++ cmd).
https://learn.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-variantinit