Home > Mobile >  Using JNA program to call self written C code in MAC
Using JNA program to call self written C code in MAC

Time:07-11

It is my g version g version

It is myself defined c code

void hello() {
  printf("Hello, World!\n");
}

I put libtest_dll.dylib below the src This is my Java code.I use maven to load jna and jna-platform

public interface IDemo extends Library {
  IDemo I_DEMO = (IDemo) Native.load("lib_test_dll", IDemo.class);
  void hello();
}
public class demo {
  public static void main(String[] args) {
System.setProperty("jna.library.path",
     "/Users/tangrunze/golang-project/go-original/tangrunze/src/");
    IDemo.I_DEMO.hello();
  }
}

When i run Java code,it's will response this.Running on win is no problem

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'test_dll':
dlopen(/Users/username/golang-project/go-original/username/src/libtest_dll.dylib, 9): no suitable image found.  Did find:
    /Users/username/golang-project/go-original/username/src/libtest_dll.dylib: mach-o, but wrong architecture
    /Users/username/golang-project/go-original/username/src/libtest_dll.dylib: mach-o, but wrong architecture
dlopen(/Users/username/golang-project/go-original/username/src/libtest_dll.dylib, 9): no suitable image found.  Did find:
    /Users/username/golang-project/go-original/username/src/libtest_dll.dylib: mach-o, but wrong architecture
    /Users/username/golang-project/go-original/username/src/libtest_dll.dylib: mach-o, but wrong architecture
dlopen(/Users/username/Library/Frameworks/test_dll.framework/test_dll, 9): image not found
dlopen(/Library/Frameworks/test_dll.framework/test_dll, 9): image not found
dlopen(/System/Library/Frameworks/test_dll.framework/test_dll, 9): image not found
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:307)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:467)
    at com.sun.jna.Library$Handler.<init>(Library.java:192)
    at com.sun.jna.Native.load(Native.java:622)
    at com.sun.jna.Native.load(Native.java:596)
    at com.trz.jna.IDemo.<clinit>(IDemo.java:7)
    at com.trz.jna.demo.main(demo.java:7)
    Suppressed: java.lang.UnsatisfiedLinkError: dlopen(/Users/username/golang-project/go-original/username/src/libtest_dll.dylib, 9): no suitable image found.  Did find:
    /Users/username/golang-project/go-original/username/src/libtest_dll.dylib: mach-o, but wrong architecture
    /Users/username/golang-project/go-original/username/src/libtest_dll.dylib: mach-o, but wrong architecture
        at com.sun.jna.Native.open(Native Method)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:197)
        ... 6 more
    Suppressed: java.lang.UnsatisfiedLinkError: dlopen(/Users/username/golang-project/go-original/username/src/libtest_dll.dylib, 9): no suitable image found.  Did find:
    /Users/username/golang-project/go-original/username/src/libtest_dll.dylib: mach-o, but wrong architecture
    /Users/username/golang-project/go-original/username/src/libtest_dll.dylib: mach-o, but wrong architecture
        at com.sun.jna.Native.open(Native Method)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:210)
        ... 6 more
    Suppressed: java.lang.UnsatisfiedLinkError: dlopen(/Users/username/Library/Frameworks/test_dll.framework/test_dll, 9): image not found
        at com.sun.jna.Native.open(Native Method)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:254)
        ... 6 more
    Suppressed: java.lang.UnsatisfiedLinkError: dlopen(/Library/Frameworks/test_dll.framework/test_dll, 9): image not found
        at com.sun.jna.Native.open(Native Method)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:254)
        ... 6 more
    Suppressed: java.lang.UnsatisfiedLinkError: dlopen(/System/Library/Frameworks/test_dll.framework/test_dll, 9): image not found
        at com.sun.jna.Native.open(Native Method)
        at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:254)
        ... 6 more

CodePudding user response:

Based on this line:

Did find: /Users/username/golang-project/go-original/username/src/libtest_dll.dylib: mach-o, but wrong architecture

You have compiled your dylib for a different architecture than what your JVM is compiled for.

“mac” as you say it, is not an architecture. Over the years it has seen many archs

  • x86-32
  • x86-64
  • arm

Not to mention the older power architecture … (not that you will see many binaries or libraries for that, but still …)

If your JVM is compiled for one of these then it will only load dylibs compiled for that specific architecure.

A 32 bit x86 process will only load 32bit x86 libs, likewise an x86-64 process will load libraries of that architecture only.

Bottom line: Make sure that you compiled your lib for the same architecture as the JVM’s.

  • Related