c - UnsatisfiedLinkError for Go native method with custom Java return type - Stack Overflow

I have a Go method that returns 2 byte arrays and 2 strings defined as: export GetDetailsfunc GetDe

I have a Go method that returns 2 byte arrays and 2 strings defined as:

// export GetDetails
func GetDetails() (unsafe.Pointer, unsafe.Pointer, *C.char, *C.char) {
    ...
    return C.CBytes(arr1), C.CBytes(arr2), C.CString(str1), C.CString(error)
}

I compile it into libmydetails.so and call this using Java code defined as:

public class MyLib {
    static {
        try {
            System.loadLibrary("mydetails");
        } catch (Exception e) {
            ...
        }
    }
    public native GoResult GetDetails();
}

public class GoResult extends Structure implements Structure.ByValue {
    public GoResult() {
    }
    public Pointer p1;
    public Pointer p2;
    public String str;
    public String error;

    protected List<String> getFieldOrder() {
        return Arrays.asList("p1", "p2", "str", "error");
    }
}

This is called by an action class as:

public class Action {
     MyLib INSTANCE = new MyLib();
     GoResult result = INSTANCE.GetDetails();
}

On executing this, libmydetails.so is successfully loaded, but I get this error while triggering the native method: java.lang.UnsatisfiedLinkError: '<pkg>.GoResult <pkg>.MyLib.GetDetails(). It looks like it's not able to map GoResult to (unsafe.Pointer, unsafe.Pointer, *C.char, *C.char) but I'm not sure what would be the best way to fix it.

When I try loading the library using Library interface instead:

public interface MyLib extends Library {
    MyLib INSTANCE = Native.load("mydetails", MyLib.class);
    
    GoResult GetDetails();
}

I'm able to run GetDetails() locally but my service requires the library to be loaded at Runtime so only System.loadLibrary("mydetails") works while running it through the service.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744724553a4590103.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信