I am building a custom module and am wanting to include a native c shared library. I'm using JNI/JNA in my module code. It's working locally when I run my code. I'm wondering the best way to package the shared library with my module when installing into ignition. Any advice or suggestions are greatly appreciated. Thanks in advance.
Package it into the JAR file (if you're using Maven or Gradle this usually just means putting it under src/main/resources
).
On module startup, extract it to a temporary directory and call System.load
, or if that isn't possible because you're using a jank dependency that doesn't offer you control over the native library loading, extract it to $IGNITION/lib/core/gateway
, which is already on java.library.path
.
This is working perfectly! Thanks for the quick response! I'm packaging the shared libraries in src/main/resources/native. These get copied to the jar file. Then on module startup, I'm able to read the library from the jar file and create a temp file. From there I have the absolute path to read in the shared library using JNA/JNI. Finally, I'm able to call native functions.
One last question. Since I'm writing a temp file which i believe is /tmp on linux and mac and C:\Users<Username>\AppData\Local\Temp\ on windows, is there anything I should watch out for? Thanks again for the response!
It might be better to use the temp directory from SystemManager::getTempDir
(SystemManager), and make sure to call deleteOnExit with your files. This ends up being $IGNITION/temp
in practice.
The problem with /tmp
on Linux is that sometimes you get a distro where the temp dir is mounted with the noexec
flag and then you can't load the libraries from there. But we've done it both ways...
Got it! Yeah I'll switch to SystemManager temp directory. I actually was using deleteOnExit already so thanks for the confirmation there.