I am working in Java on Windows OS. I’m using System.loadLibrary to load called A.dll, which exposes some JNI functions. A.dll depends on B.dll. I put A.dll and B.dll on a repo, told my colleague to pull the repo, then add the directory of the checked out repo to their PATH environment variable. The setup I have works on my machine, but my colleague gets UnsatisfiedLinkError: The OS cannot run %1.
My colleague resolved the issue by putting the directory they checked out first in their PATH environment variable, which implies they had a n incompatible version of B.dll elsewhere on their PATH environment variable. Not entirely thrilled by this solution and would like to remove the dependence of the ordering of the path environment variable. Can I ensure that Java will load B.dll from the checked out directory, as opposed to elsewhere. I’m aware you can use System.load() as well and specify a directory, but was wondering if there was another way, since afaik you would have to individually load all dependent libraries this way.
I am working in Java on Windows OS. I’m using System.loadLibrary to load called A.dll, which exposes some JNI functions. A.dll depends on B.dll. I put A.dll and B.dll on a repo, told my colleague to pull the repo, then add the directory of the checked out repo to their PATH environment variable. The setup I have works on my machine, but my colleague gets UnsatisfiedLinkError: The OS cannot run %1.
My colleague resolved the issue by putting the directory they checked out first in their PATH environment variable, which implies they had a n incompatible version of B.dll elsewhere on their PATH environment variable. Not entirely thrilled by this solution and would like to remove the dependence of the ordering of the path environment variable. Can I ensure that Java will load B.dll from the checked out directory, as opposed to elsewhere. I’m aware you can use System.load() as well and specify a directory, but was wondering if there was another way, since afaik you would have to individually load all dependent libraries this way.
Share Improve this question edited Feb 5 at 0:53 user207421 311k44 gold badges321 silver badges489 bronze badges asked Dec 19, 2024 at 15:20 aniceempanadaaniceempanada 11 bronze badge 01 Answer
Reset to default 0Dependent libraries are loaded automatically by the OS, so the JVM/Java has no control over that process. In other words, unfortunately you will be forced to work with how Windows loads libraries.
Two solutions come to mind for your particular case:
- Statically link library B into A.dll (if you are building A.dll yourself), so you end up with just a single DLL file. You will need a static version of the B library though.
- Call
SetDllDirectory
for the directory with the libraries, so Windows will find the dependent library there, and you don't have to mess around with thePATH
. (If you are on Java 22+, you can use the FFM API to do this, without needing another separate JNI library).