Unable to load native library in maven

372 views
Skip to first unread message

Vishal Thakur

unread,
May 19, 2022, 7:26:05 AM5/19/22
to Java Native Access
Hi, 
  I have a 3rd party native library (let's call it test.dylib) which i am trying to load through JNA in a maven project.
output of otool -L test.dylib  is 

/Library/Frameworks/test/Versions/A/test (compatibility version 1.0.0, current version 1.0.0)

   /System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 19.0.0)

   /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 945.11.0)

   /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

   /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 65.1.0)

   /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

   /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 744.1.0)


If i paste the .dylib file at location "/Library/Frameworks/test/Versions/A/test " from otool -L output. 
and load the file through JNA , it works fine . But if i keep the dylib under resource folder , i get error . 

java.io.IOException: Native library (darwin-x86-64/test.dylib) not found in resource path (/Users/username/Downloads/TestJNA/target/classes:

Even though i can see the dylib file present in /target/classes folder.  Can someone suggest , how can i achieve calling dylib from resource folder ? I dont want to paste the dylib file at this location "/Library/Frameworks/test/Versions/A/test" . 


This is how i am loading native library . 



public class TestJNA {

     public interface Perception extends Library {
          Perception Instance = (Perception) Native.load("test",  Perception.class);

          void method1();
           int method2(int a, int b, int c);
     }
    public static void main(String[] args) { 
           Perception.Instance.method1();
           System.out.println(Perception.Instance.method2(1,2,3));
    }
}

Matthias Bläsing

unread,
May 21, 2022, 5:00:30 AM5/21/22
to jna-...@googlegroups.com
Hi,

Am Donnerstag, dem 19.05.2022 um 04:26 -0700 schrieb Vishal Thakur:
> Hi, 
>   I have a 3rd party native library (let's call it test.dylib) which
> i am trying to load through JNA in a maven project.
> output of otool -L test.dylib  is 
> /Library/Frameworks/test/Versions/A/test (compatibility version
> 1.0.0, current version 1.0.0)
>  [...]
> java.io.IOException: Native library (darwin-x86-64/test.dylib) not
> found in resource path
> (/Users/username/Downloads/TestJNA/target/classes:
>
> Even though i can see the dylib file present in /target/classes
> folder.  Can someone suggest , how can i achieve calling dylib from
> resource folder ? I dont want to paste the dylib file at this
> location "/Library/Frameworks/test/Versions/A/test" . 

did you read the message or the documentation?

Cite from:

https://java-native-access.github.io/jna/5.11.0/javadoc/com/sun/jna/NativeLibrary.html

[...]
A search for a given library will scan the following locations:
[...]
Context class loader classpath. Deployed native libraries may be
installed on the classpath under ${os-prefix}/LIBRARY_FILENAME, where
${os-prefix} is the OS/Arch prefix returned by
Platform.getNativeLibraryResourcePrefix(). If bundled in a jar file,
the resource will be extracted to jna.tmpdir for loading, and later
removed (but only if jna.nounpack is false or not set).
[...]

And in your error:

Native library (darwin-x86-64/test.dylib) not found in resource path

So you are running on x86-64 (pre-M1) and the right path is printed there.

What are you missing?

Greetings

Matthias
Reply all
Reply to author
Forward
0 new messages