Using Native Dependencies Inside Maven

The POM dependency contains its own libraries (DLLs inside the JAR file). How to programmatically search for a path to a loaded JAR file so that I can pass it to "java.library.path"?

+2
source share
4 answers

Answering my own question: http://web.archive.org/web/20120308042202/http://www.buildanddeploy.com/node/17

In short, you can use maven-dependency-plugin: unzip the target to extract the libraries to a known path and pass this to java.library.path :

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>compile</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>org.jdesktop</groupId> <artifactId>jdic-native</artifactId> <version>${jdic.version}</version> <classifier>${build.type}</classifier> <type>jar</type> <overWrite>true</overWrite> <outputDirectory>${project.build.directory}/lib</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> 
+9
source

Since System.load() cannot load libraries from jar, you will have to use a custom loader that extracts the library to a temporary file at runtime. JNI projects discuss this approach and provide code for the custom loader.

library loader

Now we have the JNI library on the class path, so we need a way to load it. I created a separate project that will extract JNI libraries from the class path, then load them. Find it at http://opensource.mxtelecom.com/maven/repo/com/wapmx/native/mx-native-loader/1.2/ . This is added as a pom dependency, obviously.

To use this, call com.wapmx.nativeutils.jniloader.NativeLoader.loadLibrary(libname) . For more information, see javadoc for NativeLoader .

I generally prefer to wrap such things in a try / catch block like this:

 public class Sqrt { static { try { NativeLoader.loadLibrary("sqrt"); } catch (Throwable e) { e.printStackTrace(); System.exit(1); } } /* ... class body ... */ } 

An alternative would be to unpack the dependency, for example using dependency:unpack .

+2
source

You can use the maven dependency plugin to copy artifacts to a predefined path:

http://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html

0
source

If the DLL is inside the JAR, you will need to copy it to the directory before it can be loaded. (JARs that include their own libraries usually do it themselves.) If your JAR does not, you can use Class.getResourceAsStream () and write it to the directory that you added to java.library.path .

For an example of this, see loadNativeLibrary in JNA. He uses this technique to load his own library (JNI library) from the JAR.

0
source

Source: https://habr.com/ru/post/1313022/


All Articles