Jar with native libraries is not added to the runtime classpath thru module dependencies

Answered

I'm using JNA to access native libraries packaged as a jar file as per https://java-native-access.github.io/jna/4.2.1/com/sun/jna/NativeLibrary.htm.

But the native libraries cannot be found if I put the jar into module dependencies. The error is "java.lang.UnsatisfiedLinkError: Unable to load library 'citvalsrv': Native library (win32-x86-64/citvalsrv.dll) not found in resource path..."

However if I put the same jar into the 'lib' directory in the project directory, JNA is able to find it. It looks like for some reason this particular jar is not added to the system runtime classpath.

15 comments
Comment actions Permalink

Your link gives 404.

JNA library should unpack native library from the jar in order to load and use it.

Documentation at https://github.com/java-native-access/jna/blob/master/www/GettingStarted.md described how it can be loaded:

0
Comment actions Permalink

Sorry for the link, here is the right one: https://java-native-access.github.io/jna/4.2.1/com/sun/jna/NativeLibrary.html. But it does not matter, your link says the same thing. My point is, it only works when I put the jar with my native libraries into the 'lib' directory in the project root. And it does not work, when I point to the same library sitting in any other location from module dependencies. And I use Scala not Java, if it helps. 

0
Comment actions Permalink

Please share a sample project to reproduce this problem.

0
Comment actions Permalink

Ok, I think I found the problem. It has nothing to do with JNA and everything with how IDEA treats the external libraries.

If you add a directory with jars into module dependencies, they are indeed put into the system classpath.

But if you have something that IDEA thinks as a native library in the same directory (a .dll for example), the jars are not added to the classpath. Actually nothing is added to the system classpath in this case. There is no choice what type of jar it is and the directory is added as a "Native Library Locations". I have no idea what "Native Library Locations" means and what it does.

So to reproduce, just put a .dll into the directory of jars, add them as a module dependency and try to find them in the system classpath at runtime.

I have a sample project just in case, what is the best way to share it?

0
Comment actions Permalink

Why do you keep DLLs (native libraries) in the same directory as jars? JNA is able to unpack and use DLLs from the jars automatically.

0
Comment actions Permalink

It's how it comes from the Artifactory from another developer. I don't control it.

0
Comment actions Permalink

In this case you can add the jars by selecting all of them instead of using the jars directory configuration.

0
Comment actions Permalink

Adding a particular jar (not a directory of jars) to module dependencies never worked for me. Just checked in the sample project.

0
Comment actions Permalink

OK, please share a project where it doesn't work.

0
Comment actions Permalink

How am I supposed to share a project? Is there a procedure somewhere?

0
Comment actions Permalink

Create a project library, add jars to it, add the library to the module dependencies, works:

0
Comment actions Permalink

Serge Baranov,

I am working on Java Chromium Embedded Framework. I build it's native library from source code. Example apps (simple swing app) work both Win and Linux, but when I embed it into IntelliJ IDEA it crashes on Ubuntu at initialization method (works on Windows though). Error Stack trace is:

java.lang.UnsatisfiedLinkError: org.cef.CefApp.N_PreInitialize()Z
    at org.cef.CefApp.N_PreInitialize(Native Method)
    at org.cef.CefApp.access$000(CefApp.java:24)
    at org.cef.CefApp$1.run(CefApp.java:162)
    at org.cef.CefApp.<init>(CefApp.java:167)
    at org.cef.CefApp.getInstance(CefApp.java:220)
    at org.cef.CefApp.getInstance(CefApp.java:207)

I tried all the methods you listed above: jna.library.path, LD_LIBRARY_PATH, adding to class path; but neither works on Ubuntu. There is a link to full problem description: https://stackoverflow.com/questions/56554156/java-cef-intellij-plugin-support-unsatisfiedlinkerror-on-init-linux

Any feedback would be appreciated. Thank you!

0

Please sign in to leave a comment.