寻找一种从C ++调用Java的便捷方法 [英] Looking for a convenient way to call Java from C++

查看:62
本文介绍了寻找一种从C ++调用Java的便捷方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎大多数与JNI(Java本机接口)有关的文档或帮助程序库都与从Java调用本机代码有关.即使它有更多功能,这似乎也是它的主要用途.

It seems most documentation or helper libraries relating to JNI (Java Native Interface) are concerned with calling native code from Java. This seems to be the main use of it, even though it is capable of more.

我想主要朝相反的方向工作:通过向其中添加一些Java库来修改现有的(相当大的)可移植C ++程序.例如,我想让它通过JDBC调用数据库,或者通过JMS调用消息队列系统,或者发送电子邮件,或者调用我自己的Java类,等等.但是对于原始的JNI,这是非常令人不愉快的,而且容易出错.

I want to mostly work in the opposite direction: modify an existing (fairly large) portable C++ program by adding some Java libraries to it. For example, I want to make it call databases via JDBC, or message queue systems via JMS, or send emails, or call my own Java classes, etc. But with raw JNI this is pretty unpleasant and error-prone.

因此,理想情况下,我想编写可以像C ++/CLI调用CLR类一样容易地调用Java类的C ++代码.像这样:

So I would ideally like to write C++ code that can call Java classes as easily as C++/CLI can call CLR classes. Something like:

using namespace java::util::regex; // namespaces mapped

Pattern p = Pattern.compile("[,\\s]+");

array<java::lang::String> result = 
    p.split("one,two, three   four ,  five");

for (int i=0; i < result.length(); i++)
    std::cout << result[i] << std::endl;

这样,我就不必

This way, I wouldn't have to manually do the work of getting the method ID by passing the name and the weird signature strings, and would be protected from programming errors caused by the unchecked APIs for calling methods. In fact it would look a lot like the equivalent Java.

NB.我仍然会谈论使用JNI!作为一项基础技术,它非常适合我的需求.它是过程中"且高效的.我不想在单独的进程中运行Java并对其进行RPC调用. JNI本身很好.我只想要一个愉快的界面.

NB. I AM STILL TALKING ABOUT USING JNI! As an underlying technology it is perfect for my needs. It is "in process" and highly efficient. I don't want to run Java in a separate process and make RPC calls to it. JNI itself is fine. I just want a pleasant interface to it.

必须有一个代码生成工具来使等效的C ++类,名称空间,方法等完全匹配我指定的一组Java类公开的内容.生成的C ++类将:

There would have to be a code generation tool to make equivalent C++ classes, namespaces, methods, etc. to exactly match what is exposed by a set of Java classes I specify. The generated C++ classes would:

  • 具有成员函数,它们的参数接受类似包装的版本,然后执行必要的JNI伏都教来进行调用.
  • 以相同的方式包装返回值,这样我就可以自然地链接调用.
  • 维护每个类的方法ID静态缓存,以避免每次都查找它们.
  • 完全是线程安全的,可移植的,开放源代码的.
  • 在每次方法调用后自动检查异常,并生成一个std C ++异常.
  • 当我以通常的JNI方式编写本机方法时也可以使用,但是我需要调用其他Java代码.
  • 该数组应在原始类型和类之间完全一致地工作.
  • 毫无疑问,当它们需要在局部参考框架之外生存时,无疑将需要像global这样的东西来包装参考-再次,对所有数组/对象参考都应如此.

这样的免费,开源,可移植的库/工具存在吗?还是我在做梦?

Does such a free, open-source, portable library/tool exist or am I dreaming?

注意:我发现了这存在的问题,但是在那种情况下,OP并没有像我正在要求的那样完美.

Note: I found this existing question but the OP in that case wasn't nearly as demanding of perfection as I am being...

更新:关于SWIG的评论使我想到了

Update: a comment about SWIG led me to this previous question, which seems to indicate that it is mostly about the opposite direction and so wouldn't do what I want.

重要

  • 这是关于能够编写处理Java类和对象的C ++代码,而不是相反的方式(请参见标题!)
  • 我已经知道JNI存在(请参阅问题!),但是JNI API的手写代码不必要冗长,重复,容易出错,并且在编译时不进行类型检查,等等.如果要缓存方法ID和类对象更加冗长.我想自动生成C ++包装器类,这些类可以帮我解决所有问题.

更新:我已经开始研究自己的解决方案:

Update: I've started working on my own solution:

https://github.com/danielearwicker/cppjvm

如果已经存在,请告诉我!

NB.如果您正在考虑在自己的项目中使用它,请放心,但是请记住,现在的代码已经使用了几个小时,到目前为止,我只编写了三个非常费力的测试.

NB. If you're considering using this in your own project, feel free, but bear in mind that right now the code is a few hours old, and I only wrote three very unstrenuous tests so far.

推荐答案

是的,有一些现成的工具可以做到这一点-为Java类生成C ++包装器.这样可以使C ++中的Java API更加透明和令人愉悦,并且成本和风险更低.

Yes, there are existing tools that do exactly this -- generate C++ wrappers for Java classes. This makes use of Java APIs in C++ more transparent and enjoyable, with lower cost and risk.

我使用最多的是 JunC ++ ion .它是成熟,强大和稳定的.主要作者非常友善,反应迅速.不幸的是,这是一种商业产品,而且价格昂贵.

The one that I've used the most is JunC++ion. It's mature, powerful and stable. The primary author is very nice, and very responsive. Unfortunately, it's a commercial product, and pricey.

Jace 是具有BSD许可证的免费开放源代码工具.自从我上一次与jace一起玩已经好几年了.看起来仍有一些积极的发展. (我仍然记得十年前的原始作者的USENET帖子,提出的问题基本上与您要问的相同.)

Jace is a free, open-source tool with a BSD license. It's been years since I last played with jace. Looks like there's still some active development. (I still remember the USENET post by the original author, over a decade ago, asking basically the same question you're asking.)

如果需要支持从Java到C ++的回调,定义实现Java接口的C ++类将很有帮助.至少JunC ++ ion允许您将此类C ++类传递给采用回调的Java方法.我上次尝试使用jace时,它不支持此功能-但这是七年前.

If you need to support callbacks from Java to C++, it's helpful to define C++ classes that implement Java interfaces. At least JunC++ion allows you to pass such C++ classes to Java methods that take callbacks. The last time I tried jace, it did not support this -- but that was seven years ago.

这篇关于寻找一种从C ++调用Java的便捷方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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