-->

How to include native library on maven's java.

2020-06-03 02:44发布

问题:

I am trying to use JNotify for my application , which has the following requirements

JNotify can be tested by simply running the jar file with the followng commend: java -Djava.library.path=. -jar jnotify-VER.jar [dir]

JNotify will then monitor the specified dir (or the current directory if dir is not specified) and print detected events. Note that java.library.path should point to the location of the native libraries that comes with jnotify (dlls, so dylibs etc).

But trying to get the same thing working with maven is not working out. I am trying to run a simple test but I get the following error

    java.lang.UnsatisfiedLinkError: no jnotify in java.library.path
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
    at java.lang.Runtime.loadLibrary0(Runtime.java:823)
    at java.lang.System.loadLibrary(System.java:1028)
    at net.contentobjects.jnotify.linux.JNotify_linux.<clinit>(Unknown Source)
    at net.contentobjects.jnotify.linux.JNotifyAdapterLinux.<init>(Unknown Source)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)        

which means the native files are not found on the library path.

My pom.xml looks like this -

I have added the jar and .so to our internal repository

<dependency>
            <groupId>net.contentobjects</groupId>
            <artifactId>jnotify</artifactId>
            <version>0.93</version>
</dependency>
<dependency>
           <groupId>net.contentobjects</groupId>
           <artifactId>jnotify</artifactId>
           <version>0.93</version>
           <type>so</type>
</dependency>


<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <configuration>
            <argLine>-Djava.library.path=target/lib/</argLine>
     </configuration>
</plugin>

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-dependency-plugin</artifactId>
     <executions>
       <execution>
              <id>copy</id>
              <phase>compile</phase>
              <goals>
                     <goal>copy</goal>
              </goals>
              <configuration>
                    <artifactItems>
                      <artifactItem>
                             <groupId>net.contentobjects</groupId>
                             <artifactId>jnotify</artifactId>
                             <version>0.93</version>
                             <type>so</type>
                             <overWrite>true</overWrite>
                      <outputDirectory>${project.build.directory}/lib</outputDirectory>
                     </artifactItem>
                     </artifactItems>
               </configuration>
             </execution>
          </executions>
 </plugin>

This doesn't work though, any ideas whats missing?

thanks

回答1:

Does the dependency .so file get copied into the target directory in the compile phase? Is it named correctly? Likely it will be named something like jnotify-0.93.so.

If you need it named just jnotify.so, you might want to tryt to turn on the stripVersion option of the maven-dependency-plugin's copy goal.



标签: maven native