从 C++ 访问 Java 类的最佳方式? (比直接使用JNI更好)
我必须将一个大型 Java 库集成到一个相当大的 C++ 应用程序中。 解决方案是使用 JNI,但这需要手动编写所有类的代码。 例如,Python 有一个很棒的 JPype 解决方案(http://jpype.sourceforge.net/)使该过程自动化(尽管由于 C++ 和 Python 不同的性质,相同的解决方案不能应用于 C++)。
谢谢,
达斯
I have to integrate a large Java library in a quite large C++ application. A solution is to use JNI but this requires to hand code all the classes. Python has, for example, a wonderful solution with JPype (http://jpype.sourceforge.net/) that automatizes the process (although the same solution cannot be applied to C++ due to C++ and Python different natures).
Thanks,
Das
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
SWIG 是一个工具,可让您自动生成从一种语言到另一种语言的绑定。 它支持 C++ 和 Java 以及其他十几种语言。
SWIG is a tool that lets you auto-generate bindings from one language to another. It supports C++ and Java and a dozen other languages.
看来我的问题还不够清楚。 也许混乱来自允许在展位方向访问的 JNI...
我想做的是从 C++ 访问 Java 库。 也就是说,有人给我一个 JAR 文件,其中包含 JAVA 编译类的集合,我必须用 C++ 编写代码才能访问它(因为我想将该 Java 库的功能集成到 C++ 应用程序中)。
希望这能澄清访问方向:)
谢谢,
Das
Seems that my question was not clear enough. Maybe the confusion comes from JNI that allows the access in booth directions ...
What I want to do is to access a Java library FROM C++. That is, someone give me a JAR file that contains a collection od JAVA compiled classes and I must write code in C++ to access it (as I want to integrate the functionality of that Java library into a C++ application).
Hope that this clarifies the direction of access :)
Thanks,
Das
不确定这个商业工具是否让生活变得更轻松,但您可能想探索 - http:// www.teamdev.com/jniwrapper/index.jsf
Not sure if this commercial tool makes life any easier, but you might wanna explore -- http://www.teamdev.com/jniwrapper/index.jsf
JNA 并不完全是您想要的,但它确实让您的生活变得更轻松。 它不需要任何样板/生成的代码 - 您只需为要调用的方法编写一个接口。
当然,有充分的理由使用 JNA 而不是 JNI。
JNA is not quite what you're looking for, but it does make your life a lot easier. It doesn't require any boilerplate/generated code - you just write an interface for the methods you want to call.
Certainly, there is every reason to use JNA instead of JNI.
我有两个建议,可能适合你,也可能不适合你:
首先,你可以尝试一些非常简单的事情。 在单独的进程中运行 Java 代码,并通过管道或套接字与其进行通信。 这相当容易做到,并且不需要任何疯狂的库。 缺点是通信受到一定限制(只有一些简单的管道),您需要编写自己的包装器来发送数据 其次
,这个库到底是什么? 也许我们可以建议不是用 Java 编写的替代方案,这样在您的 C++ 应用程序中使用会更容易?
I have two suggestions which may or may not work for you:
First, you could try something very simple. Run the Java code in a separate process, and communicate with it with pipes or sockets. This is fairly easy to do, and doesn't require any crazy libraries. The downside is that the communication is somewhat limited (just some simple pipes), you'll need to write your own wrapper around it to send data across
Secondly, what exactly is this library? Perhaps we can suggest alternatives that are not written in Java, that would be much easier to use in your C++ application?