最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Is there a way to ensure System.loadLibrary loads dependent DLLs from the same directory as the primary loaded DLL - Stac

programmeradmin2浏览0评论

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 0
Add a comment  | 

1 Answer 1

Reset to default 0

Dependent 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:

  1. 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.
  2. 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 the PATH. (If you are on Java 22+, you can use the FFM API to do this, without needing another separate JNI library).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论