'How to handle "java.lang.UnsatisfiedLinkError" when the library path is satisfied?

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?



Solution 1:[1]

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://docs.microsoft.com/en-us/windows/win32/api/oleauto/nf-oleauto-variantinit

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gurusivaram K