Hello,
We are using the nar plugin for the first time and the features are promising !!!
We are trying to generate a nar file and integrate this by using JNA which has its own way to load shared libraries.
We have encountered many difficulties but at the end but we found a way…
Still we have to do some modifications in the generated NarSystem class. We would like to avoid this bad practice (editing generated code), so we might need your help.
We have seen that once NarSystem.loadLibrary() is invoked, Java have the expected entry in Thread.currentThread().getContextLoader() nativeLibraies.
So it sounds like the library is found and loaded.
Next, we call the JNA Native library :
myVariable= Native.load(“the name as defined in NarSystem”, MyInterface.class);
Note that we have to copy/paste the filename value.
As we would like to avoid this, do you think it would be possible to make the filename public static so we could use it?
We found that modification could be made at https://github.com/maven-nar/nar-maven-plugin/blob/2c3442e3d1045907b44660d83399d51fbd612a75/src/main/java/com/github/maven_nar/NarSystemMojo.java#L200.
Still, it did not work because the JNA library did not see? use ? Thread.currentThread().getContextLoader() nativeLibraries parameter.
So we modify again the NarSystem generated class to extract the loaded path :
String absolutePath;
if (unpacked != null) {
absolutePath= unpacked.getPath();
} else try {
final String libPath = getLibPath(loader, aols, mappedNames);
final JniExtractor extractor = new DefaultJniExtractor(NarSystem.class);
final File extracted = extractor.extractJni(libPath, fileName);
absolutePath = extracted.getAbsolutePath();
} catch (final Exception e) {
e.printStackTrace();
throw e instanceof RuntimeException ?
(RuntimeException) e : new RuntimeException(e);
}
System.load(absolutePath);
return absolutePath;
With that we finally make it work with the following code :
String path = NarSystem.loadLibrary();
NativeLibrary.addSearchPath(NarSystem.fileName, Path.of(path).getParent().toString());
Do you think this is the good solution ? We would be ready to contribute if so.
If not, do you have any other solution to our problem ?
Best regards,
--
Anne-Laure LUGAN