I used Eclipse and built a JNI project on my development machine using the nar-maven plugin. That compiles on multiple architectures and leaves me with .nar files.
My problem is that now I want to run the main program in one of the Java classes in the nar. I keep getting an UnsatisfiedLinkError when the Java program tries to load the native library that should be in the nar file. Now I realize that the .so file needs to be on the java.library.path but my problem is that I cannot see where to make that point since the only copy is somehow inside the nar file. I was under the impression that this would be handled behind the scenes because I'm using the auto-generated NarSystem.loadLibrary method in the Java.
Details follow.
The part of my POM configuring the nar plugin:
<plugin>
<groupId>com.github.maven-nar</groupId>
<artifactId>nar-maven-plugin</artifactId>
<version>3.5.1</version>
<extensions>true</extensions>
<configuration>
<libraries>
<library>
<type>jni</type>
<narSystemPackage>com.myco.package</narSystemPackage>
</library>
</libraries>
</configuration>
</plugin>
In one of my Java classes, I have a static initializer that calls the auto-generated method for loading the library:
static {
com.myco.package.NarSystem.loadLibrary();
}
After running mvn install, I have the following files in the appropriate sub-directory of my local .m2/repository directory:
maven-metadata-local.xml_maven.repositoriesproject-0.0.1-arm-Linux-gpp-jni.narproject-0.0.1.narproject-0.0.1.pomproject-0.0.1-javadoc.jar
I try to run from the command line with this command:
java -cp /home/myname/.m2/repository/com/myco/project/0.0.1/project-0.0.1.nar com.myco.package.MainClass
That begins to run the appropriate main program, so it found the Java portion. When it gets to the point where it should load the native library - the static block above - it throws the error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no project-0.0.1 in java.library.path
This question, Nar dependency in multi-module maven project, looked like it would be close, but the answer depends on already having the native library "exposed" somehow. I followed the questioner there trying mvn nar:nar-unpack and mvn exec:java, but that did not advance me. (Get the same apparently null result from the first as described in that question, and the same exception that I described here for the second.)
Other questions considered:
- Maven - UnsatisfiedLinkError with java.library.path Seems to be about finding libraries outside of the maven process. Answer given in terms of maven configuration files, which doesn't seem to help once I'm deployed.
- maven-nar-plugin and tests in dependent module This deals with an issue specific to running tests.
- nar-maven-plugin and native-library-loader dont load native lib This one is close but in any case has no answer. It seems to be different since that poster was trying to specify a library path on the command line somehow.
- Using a native Maven artifact (nar) in a webapp This one is about incorporating into a webapp, so it's different. It does seem to offer this clue: "Both of these are produced as NAR artifacts and require the maven-nar plugin in order to be used." But I haven't figured out how the plugin needs to be used!
So how do I run this main program from the command line? If there's some pre-processing step to "extract" the native library what is it? If it's automatic, then what am I missing? Thanks!
I got a tip and finally got it!
This dependency needs to be added to the POM:
After that I could run it using
Trying to run it directly with
javafrom the command line still crashed, but in this case it crashed because it wasn't finding the jars for the dependencies. Presumably I could run it now without maven if I were to specify class path entries for all of them with the-cpflag.