如何处理“java.lang.UnsatisfiedLinkError”当库路径满足时?

发布于 2025-01-10 17:17:40 字数 2258 浏览 0 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零崎曲识 2025-01-17 17:17:40

您应该为代码中使用的标头传递相应的 .dll 文件名。

例如。在 C++ 中,如果我添加以下头文件,

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <stdio.h>
#include <lm.h>
#include <jni.h>
#include <iostream>
#include <activeds.h>
#include <wchar.h>
#include <objbase.h>

#pragma comment(lib, "netapi32.lib")

那么我的 g++ cmd 将会是,

g++ -shared -o prefNameOfDllFIle.dll cppCodeObjectFile.o -ladsiid -lactiveds -lnetapi32 -lole32 -loleaut32

您可以通过搜索特定 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,

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <stdio.h>
#include <lm.h>
#include <jni.h>
#include <iostream>
#include <activeds.h>
#include <wchar.h>
#include <objbase.h>

#pragma comment(lib, "netapi32.lib")

Then my g++ cmd would be,

g++ -shared -o prefNameOfDllFIle.dll cppCodeObjectFile.o -ladsiid -lactiveds -lnetapi32 -lole32 -loleaut32

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文