Including the *.SO library to the Java project (Li

2019-08-22 08:44发布

I am not a Java developer, but I need to solve a problem: I need to include linux SO library to an existing Java project. The library is developed using CGO and works fine with C++, Python ctypes, Ruby FFI and so on. But I can not include it to Java project without of errors (java.lang.UnsatisfiedLinkError). I have read some articles like this, and the described method is that I need use javah to create a C header first, and then write a C program, and so on. But what to do, if I have already compiled *.SO file? Is there a way, how to simply load an existing SO file (written on C) and call it`s functions?

1条回答
Fickle 薄情
2楼-- · 2019-08-22 09:01

Java doesn't have builtin FFI functionality.

One option for using native libraries (.dll/.so) with Java is to write a JNI wrapper library that has special functions that can be bound to Java native methods. That's the option where you need to use javah to create a header file for the wrapper library. See the "Java Native Interface" documentation on Oracle's site for how to do that.

The other approach is to use a "glue" library like JNA. You don't need to build another library this way but you need to include JNA in your project and do the necessary Java declarations for it. You can find the documentation for JNA in the Github repository together with the code. That approach is similar to what Python, Ruby, etc. are doing.

I recommend reading up on both to see what will better suit your needs.

查看更多
登录 后发表回答