What is best practice to call `SymbolLookup.libraryLookup()` in cross-platform way?
Jorn Vernee
jorn.vernee at oracle.com
Fri Jan 30 13:42:12 UTC 2026
Hello,
SymbolLookup.libraryLookup() calls the underlying lookup mechanism of the OS, so it is different on each platform. Note that it doesn't use the -Djava.library.path option, and instead uses the OS search path. On Linux this is LD_LIBRARY_PATH, on Mac DYLD_LIBRARY_PATH, and on Windows PATH (though on Windows there are other locations that are searched [1]).
But, it sounds like you probably want System.loadLibrary/load instead. These methods use -Djava.library.path for the search, and associate the loaded library with the caller's class loader. From there, symbols can be looked up in the loaded library using the lookup returned by SymbolLookup.loaderLookup() [2]
Hope that helps,
Jorn
[1]: https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order
[2]: https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/foreign/SymbolLookup.html#loaderLookup()
On 29-1-2026 16:13, Lev Serebryakov wrote:
> Hello!
>
> I wonder, is there best practices for `SymbolLookup.libraryLookup()`?
>
> I mean, in POSIX-like systems it look in system directories and I can simply pass `"libname"` as argument. But on Windows it doesn't work even with `-Djava.library.path` which points do directory with `libname.dll`, looks like I need ".dll" to name.
>
> Check for OS via property "os.name" looks like bad solution: maybe, on other systems, which I'm not ware about, I need add other extension, and my code which check will fails!
>
> And I cannot understand what should I pass in MacOS. I've tried "name", "libname", "libname.dylib" — nothing works. Library file is "libname.dylib" in directory passed as `-Djava.library.path".
>
> Is here better and future-proof way?
>
> I can copy code from JNA, but I don't like it very much, to be honest.
>
> Thnak you.
>
More information about the panama-dev
mailing list