Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a JNI DLL search for its dependent native DLL?

Say I have JNI.dll. It depends on native.dll. Now my Java application calls System.loadLibrary("JNI").

Will the following folder layout work?

MainFolder
    |--main.exe
    |--SubFolder
          |--JNI.dll
          |--native.dll

My guess is, there are 2 levels of dependency resolution.

[Level 1]:

System.loadLibrary("JNI") uses JVM property java.library.path to locate JNI.dll.

[Level 2]:

JNI.dll relies on Windows system mechanism to locate native.dll.

Is this correct?

If I set %_JAVA_OPTIONS% as -Djava.library.path=MainFolder\SubFolder, I think it can cover the search for JNI.dll. But will it cover the search for native.dll, too?

ADD 1

It seems my guess of 2 levels got confirmed from here: How to add native library to "java.library.path" with Eclipse launch (instead of overriding it)

See comment by kevin cline. But the mentioned solution with LD_LIBRARY_PATH environment variable only applies to Linux.

ADD 2

I think I didn't make my question clear. Let me put it this way.

My confusion is: the JNI.dll depends on native.dll. Both of them are not in the main.exe's current working directory. Actually they are in a sub folder of CWD.

If I run main.exe directly, I just need to set the java.library.path = <other path>\MainFolder\SubFolder. Both DLL are found correctly.

But if I run my project from Eclipse, besides setting the java.library.path, I have to put the "\MainFolder\SubFolder" in the %PATH% environment variable.

I just don't know why Eclipse is so different.

like image 350
smwikipedia Avatar asked Nov 09 '22 15:11

smwikipedia


1 Answers

1)It searches in the current Directory.

2)System folder typically C:\Windows\System32 (use CSIDL_SYSTEM with shgetspecialfolderpath() to get the rigth folder on the given system)

3)Windows folder (C:\Windows )(CSIDL_WINDOWS with shgetspecialfolderpath() to get the right folder on the given system)

4)All the folders listed under PAth environmnet variable

like image 60
GingerJack Avatar answered Nov 14 '22 22:11

GingerJack