Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include native library on maven's java.library.path variable

Tags:

maven

native

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

like image 893
kelly Avatar asked Aug 16 '11 02:08

kelly


People also ask

Where is Java native library?

LIB/MYLIB. LIB is the path that contains the native library you want to load using the System. loadLibrary() call, and myclass is the name of your Java application. For information about how a native library is located by the System.

What is Native library location in eclipse?

Go to Project properties->Java Build Path->Source. You'll find a list of source-folders. Each entry under the the Source tab has Native library locations. It supports paths within the workspace and it will make Eclipse add it to your java.


1 Answers

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.

like image 88
prunge Avatar answered Sep 24 '22 23:09

prunge