将* .SO库包括到Java项目(Linux)中 [英] Including the *.SO library to the Java project (Linux)

查看:108
本文介绍了将* .SO库包括到Java项目(Linux)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是Java开发人员,但是我需要解决一个问题:我需要将linux SO库包含到现有的Java项目中.该库是使用CGO开发的,可以与C ++,Python ctypes,Ruby FFI等配合使用.但是我不能在没有错误的情况下将它包含到Java项目中(java.lang.UnsatisfiedLinkError).我已经阅读了一些文章,例如 ,并且所描述的方法是,我需要先使用javah创建C头,然后编写C程序,依此类推.但是,如果我已经编译了* .SO文件,该怎么办? 有没有办法,如何简单地加载现有的SO文件(用C编写)并调用其功能?

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?

推荐答案

Java没有内置的FFI功能.

Java doesn't have builtin FFI functionality.

将本机库(.dll/.so)与Java一起使用的一个选项是编写JNI包装器库,该库具有可以绑定到Java本机方法的特殊功能.这是您需要使用javah为包装器库创建头文件的选项.有关如何执行此操作,请参见Oracle网站上的"Java本机接口"文档.

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.

另一种方法是使用胶水"库,例如 JNA .您不需要以这种方式构建另一个库,但是您需要在项目中包括JNA并为此进行必要的Java声明.您可以在Github存储库中找到有关JNA的文档以及代码.这种方法类似于Python,Ruby等.

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.

这篇关于将* .SO库包括到Java项目(Linux)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆