DLL file path problem when IDEA uses JNA

< H1 > 1. Problem description < / H1 >

JNA technology is used because work requires calling DLL, in the SDK provided by a third party. The development steps and the use of JNA have been familiar, and the development task has been completed, but there has always been a question: where on earth should the storage path of the DLL file in IDEA be placed?

< H1 > 2. Try < / H1 >

first of all, let"s introduce the catalogue of the project:

:IDEA JDK 1.8 32bit
:   ---
         --- ... 
         --- lib Modules  Dependencies  lib 
         --- src  Sources 
         --- ... 

the DLL provided by the third party is 32bit, and finally successfully calls the third party"s DLL to implement the requirements, so the problem of bit incompatibility is eliminated.

  1. lib directory of the project:

    Caused by: java.lang.UnsatisfiedLinkError: Unable to load library "dllName": 
    Native library (win32-x86/dllName.dll) not found in resource path 
    ([file:/C:/Program%20Files%20(x86)/Java/jdk1.8.0_171/jre/lib/charsets.jar, 
    ......

    as above, there are two paths in the error report. I have tried to prevent the DLL file from being in the corresponding directory, but the error report is still the same

    .
  2. the src directory of the project:
    is placed in the src directory. After testing, the DLL file can be loaded normally. Because of the directory structure of the project, it is definitely not possible to put it in the src directory. Ideally, it should be placed in the lib directory
  3. .
  4. in the C:Program Files (x86) Javajdk1.8.0_171jrebin directory:
    in this directory is also the
  5. where DLL files can be loaded normally.
  6. in the C:WindowsSystem32 directory:
    SDK development documentation says you can try to put DLL files in that directory, but testing on IDEA is not valid.
  7. modify the VM option in the IDEA running configuration to:-Djava.library.path=D:\ *\ lib

    this method is of no use either

  8. use absolute path
    using absolute path does not load the DLL file correctly, and the absolute path does not meet the requirements
< H1 > 3. Question you want to ask: < / H1 >

how to set the path to the DLL file in IDEA (in addition to the src directory), and in the IDE environment, how to ensure that the program loads the DLL file correctly at run time?

Mar.20,2021

just load the dynamic link library according to the library name:

first, jni,jni loads dynamic link libraries only from the path set by JVM system attribute java.library.path, but the default values of this variable under windows, linux and mac are different. If you want to customize the path, it is easiest to append it in PATH, while mac and linux are not so simple. You can also append the path to java.library.path by setting system variables such as LD_LIBRARY_PATH. However, this variable has been discarded in some systems, so mac and linux recommend setting the custom library path through-Djava.library.path=xxx.

For

JNA, you can find it from the system library by default. Windows is the same as JNI, and you can append it to PATH. Under linux and mac, you need to use-Djna.library.path=xxx to configure. Of course, you can also set it as a global library. Take ubuntu as an example: create a new file / etc/ld.so.conf.d/xxx.conf, xxx.conf to add a custom library path, and finally run the command sudo ldconfig.

if you set it this way, you don't need to care about the impact of development tools.

Menu