寻找一种从 C++ 调用 Java 的便捷方法;

发布于 2024-12-06 01:52:37 字数 2234 浏览 0 评论 0 原文

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

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

因此,我理想地希望编写可以像 C++/CLI 调用 CLR 类一样轻松地调用 Java 类的 C++ 代码。像这样:

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;

这样,我就不必 通过传递名称和奇怪的签名字符串来手动完成获取方法ID的工作,并且可以避免因调用方法的未经检查的API而导致的编程错误。事实上,它看起来很像等效的 Java。

注意。我仍在谈论使用 JNI! 作为一项底层技术,它非常适合我的需求。它是“进行中”并且非常高效。我不想在单独的进程中运行 Java 并对它进行 RPC 调用。 JNI本身没问题。我只是想要一个令人愉快的界面。

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

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

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

注意:我发现这个现有问题< /a> 但在这种情况下,OP 并不像我那样要求完美......

更新: 关于 SWIG 的评论让我上一个问题,这似乎表明它主要是关于相反的方向,所以不会做我想做的事。

重要

  • 这是关于能够编写操作 Java 类和对象的 C++ 代码,而不是相反(参见标题!)
  • 我已经知道 JNI 存在(参见问题!)但是手-向 JNI API 编写的代码不必要地冗长、重复、容易出错、在编译时未进行类型检查等。如果您想缓存方法 ID 和类对象,那就更冗长了。我想自动生成 C++ 包装类来为我处理所有这些事情。

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

https://github.com/ danielearwicker/cppjvm

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

注意。如果您正在考虑在自己的项目中使用它,请随意,但请记住,现在代码已经发布了几个小时,到目前为止我只编写了三个非常不费力的测试。

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.

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.

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. 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.

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:

  • Have member functions that accept similarly-wrapped versions of their parameters and then do the necessary JNI voodoo to make the call.
  • Wrap the return values in the same way so I can chain calls in a natural way.
  • Maintain a per-class static cache of method IDs to avoid looking up them every time.
  • Be totally thread-safe, portable, open source.
  • Automatically check for exceptions after every method call and produce a std C++ exception.
  • Also work for when I'm writing native methods in the usual JNI way but I need to call on to other Java code.
  • The array should work totally consistently between primitive types and classes.
  • Will no doubt need something like global to wrap references in when they need to survive outside of a local reference frame - again, should work the same for all array/object references.

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

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

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.

IMPORTANT

  • This is about being able to write C++ code that manipulates Java classes and objects, not the other way round (see the title!)
  • I already know that JNI exists (see the question!) But hand-written code to the JNI APIs is unnecessarily verbose, repetitious, error-prone, not type-checked at compile time, etc. If you want to cache method IDs and class objects it's even more verbose. I want to automatically generate C++ wrapper classes that take care of all that for me.

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

https://github.com/danielearwicker/cppjvm

If this already exists, please let me know!

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

狼性发作 2024-12-13 01:52:37

是的,有一些现有的工具可以做到这一点——为 Java 类生成 C++ 包装器。这使得在 C++ 中使用 Java API 更加透明和愉快,并且成本和风险更低。

我最常用的是 JunC++ion。它成熟、强大、稳定。主要作者非常好,而且非常敏感。不幸的是,它是一种商业产品,而且价格昂贵。

Jace 是一款具有 BSD 许可证的免费开源工具。自从我上次和杰斯一起打球已经很多年了。看起来仍有一些积极的发展。 (我仍然记得十多年前原作者在 USENET 上发表的文章,提出了与您基本相同的问题。)

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

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.

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 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.)

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.

断念 2024-12-13 01:52:37

我是 Codemesh 语言集成产品(包括 JunC++ion)的主要架构师之一。我们自 1999 年以来一直在进行这种集成,效果非常好。最大的问题不是JNI部分。 JNI 很乏味且难以调试,但是一旦你正确使用,它通常会继续工作。有时,您会因 JVM 或操作系统更新而崩溃,然后您必须微调您的产品,但总的来说它是稳定的。

最大的问题是类型系统映射以及一般可用性和目标解决方案之间的权衡。例如,您声明您不喜欢 JACE 将所有对象引用视为全局变量这一事实。我们做了同样的事情(有一些逃生舱口),因为事实证明,这种行为最适合 95% 的客户,即使它会损害性能。如果您要发布 API 或产品,则必须选择适合大多数人的默认设置。选择本地引用作为默认选项是错误的,因为越来越多的人正在编写多线程应用程序,并且人们想要从其他语言使用的许多 Java API 本质上是带有异步回调等的多线程。

我们还发现您确实希望为人们提供一个基于 GUI 的代码生成器来创建集成规范。一旦他们指定了它,您就可以使用 CLI 版本将其集成到夜间构建中。

祝你的项目好运。要做到正确需要做很多工作。我们在这方面花费了几年的时间,并且仍在定期改进。

I'm one of the prinicpal architects for Codemesh's language integration products, including JunC++ion. We have been doing this kind of integration since 1999 and it works really well. The biggest problem is not the JNI part. JNI is tedious and hard to debug, but once you get it right, it mostly just keeps working. Every now and then, you get broken by a JVM or an OS update, and then you have to fine-tune your product, but in general it's stable.

The biggest problem is the type system mapping and the trade-offs between general usability and targeted solution. You state for example that you don't like the fact that JACE treats all object references as globals. We do the same thing (with some escape hatches) because it turns out that this is the behavior that works best for 95% of customers, even if it hurts performance. If you're going to publish an API or a product, you have to pick the defaults that make things work for most people. Picking local references as the default option would be wrong because more and more people are writing multithreaded applications, and a lot of Java APIs that people want to use from other languages are intrinsically multithreaded with asynchronous callbacks and the like.

We also found out that you really want to give people a GUI-based code generator to create the integration specification. Once they've specified it, you use the CLI version to integrate it into the nightly build.

Good luck with your project. It's a lot of work to get right. We spent several years on it and we're still making it better regularly.

是伱的 2024-12-13 01:52:37

我遇到了几乎相同的问题,最终自己做了,也许这对某人有帮助。

https://github.com/mo22/jnipp

它的运行时占用空间很小(<30kb),管理引用,并支持生成Java类接口。
即LocalRef>字符串数组;然后使用 stringArray[1]->getBytes() 或其他东西。

I had pretty much the same problems, ended up doing it on my own, maybe this helps someone.

https://github.com/mo22/jnipp

It has a small runtime footprint (<30kb), manages references, and supports generating Java class interfaces.
I.e. LocalRef> stringArray; and then using stringArray[1]->getBytes() or something.

讽刺将军 2024-12-13 01:52:37

从 C++ 重新调用 Java。

你可以做你想做的事,但你必须让 Java 来控制。我的意思是,您创建调用本机代码的 Java 线程,并从那里它们阻塞,等待您的本机代码给它一些事情做。您可以根据需要创建任意数量的 Java 线程来完成足够的工作/吞吐量。

因此,您的 C++ 应用程序启动时,它会创建一个 JVM/JavaVM(按照记录的方式,示例存在于 qtjambi 代码库中,请参见下文),这反过来会执行通常的 JNI 初始化和 System.loadLibrary() 并为 JAR 提供“本机”连锁。然后,您初始化一堆线程并调用一些 JNI 代码(您创建的),它们可以在其中阻塞等待您的 C++ 代码给它们一些工作要做。

然后,您的 C++ 代码(可能来自另一个线程)设置并将所需的所有信息传递给阻塞和等待的 Java 线程工作线程之一,然后给出运行命令,然后它可能会返回到纯 Java 代码中执行操作工作并带着结果回来。

...

可以从 C++ 代码设置、创建和包含 JavaVM 实例。这可以强制输入您自己的 CLASSPATH/JAR,以设置您需要封装在 C++ 程序中的包含环境。

我确信您已经在 http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/invocation.html

...

有一种C++ => QtJambi 项目中的 Java JNI 生成器(我参与并帮助维护)。这是为 Qt 工具包定制的,但本质上它将一堆 C++ 头文件转换为 C++ .cpp/.h 文件和 *.java 文件的集合,以提供对象的链接和 shell 包含以便竞争的内存分配方案能够很好地协同工作。或许我们可以从中得到一些启示。

这无疑证明了您所要求的生成器恰好包含在 qtjambi 项目中(但可以通过一些工作使其独立),并且这是 LGPL 许可的(开源)。 Qt 工具包不是一个小 API,但它可以生成数百个类来覆盖高比例的 API(> 85%,几乎 100% 的 Core/GUI 部分)。

华泰

Re calling Java from C++.

You can do what you wish but you must let Java be in control. What I mean by this is that you create Java threads that call into Native code and from there they block, kind of waiting for your native code to give it something to do. You create as many Java threads as you need to get enough work / throuhput done.

So your C++ application starts up, it creates a JVM/JavaVM (as per the documented way, example exists in qtjambi codebase see below), this in turn perform the usual JNI initialization and System.loadLibrary() and provides JARs with "native" linkage. You then initialize a bunch of threads and call some JNI code (that you created) where they can block in wait for your C++ code to give them some work to do.

Your C++ code (presumabily from another thread) then sets up and passes all the information needed to one of the blocked and waiting Java Thread workers, it is then given the order to run, it may then go back into pure-Java code to do work and return with a result.

...

It is possible to setup and create and contain a JavaVM instance from C++ code. This can be force fed your own CLASSPATH/JARs to setup the contained environment you need encapsulated inside your C++ program.

Outline of that as I'm sure you have found already at http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/invocation.html

...

There is a kind of C++ => Java JNI generator in the QtJambi project (that I work on and help maintain). This is quite bespoke for the Qt toolkit but essentially it translates a bunch of C++ header files into a collection of C++ .cpp/.h files and *.java file to provide linkage and shell containment of the object so that the competing memory allocation schemes play well together. Maybe there is something to be taken from this.

This is certainly a proof in cencept for what you are asking the generator just happens to be contained in the qtjambi project (but could be made independant with some work) and this is LGPL licensed (open-source). The Qt toolkit is not a small API yet it can generated 100s of classes to cover high % of API (>85% and almost 100% of Core/GUI parts).

HTH

无妨# 2024-12-13 01:52:37

我也遇到了很多困难
让 JNI 能够处理不同的情况
操作系统,应对 32/64 位体系结构并确保找到并加载正确的共享库。
我发现 CORBA(MICO 和 JacORB)也很难使用。

我没有找到从 C/C++ 调用 Java 的有效方法以及在这种情况下我的首选解决方案
将我的 Java 代码运行为:

  1. 独立程序
    我可以轻松地从 C/C++ 程序运行
    使用 java -cp myjar.jar org.foo.MyClass 。我想这对于您的情况来说太简单了。

  2. 作为迷你服务器,接受来自C/C++的请求
    TCP/IP 套接字上的程序和
    也通过此套接字返回结果。
    这需要编写网络和序列化函数
    但是解耦了C/C++和Java进程,你可以清楚地
    识别任何问题是在 C++ 端还是 Java 端。

  3. 作为 Tomcat 中的 Servlet 并从我的 C/C++ 发出 HTTP 请求
    程序(其他 servlet 容器也可以工作)。
    这还需要编写网络和序列化函数。
    这更像是 SOA。

I also had many difficulties
getting JNI to work on different
operating systems, coping with 32/64-bit architectures and making sure the correct shared libraries were found and loaded.
I found CORBA (MICO and JacORB) difficult to use too.

I found no effective way to call from C/C++ into Java and my preferred solutions in this situation
are to run my Java code as either:

  1. a stand-alone program
    that I can easily run from C/C++ programs
    with java -cp myjar.jar org.foo.MyClass. I guess this is too simplistic for your situation.

  2. As a mini-server, accepting requests from C/C++
    programs on a TCP/IP socket and
    returning results through this socket too.
    This requires writing networking and serializing functions
    but decouples the C/C++ and Java processes and you can clearly
    identify any problems as being in the C++ side or Java side.

  3. As a Servlet in Tomcat and make HTTP requests from my C/C++
    program (other servlet containers would also work too).
    This also requires writing networking and serializing functions.
    This is more like SOA.

╰沐子 2024-12-13 01:52:37

使用 Thrift协议缓冲区 来促进 Java 到 C++ 的调用?

What about using Thrift or Protocol Buffers to facilitate your Java to C++ calls?

辞旧 2024-12-13 01:52:37

对于这个钉子来说,可能有点太大的锤子,但这不是 CORBA 的构建方式为了?

Maybe a bit of a too large hammer for this nail, but isn't that what CORBA was built for?

是伱的 2024-12-13 01:52:37

由于 CORBA 似乎不是您想要的,这里有一些链接描述如何从 C/C++ 启动 JVM 并调用 JAVA 方法。

http://java.sun.com/docs/books/jni/html /invoke.html

http://java.sys-con.com/node/45840

PS:连接时Java 与 C++ 您还应该看看 JNAbridj

Since CORBA doesn't seem to be what you want, here are links describing how to start JVM from C/C++ and calling JAVA-methods.

http://java.sun.com/docs/books/jni/html/invoke.html
and
http://java.sys-con.com/node/45840

PS: When interfacing Java with C++ you should also have a look at JNA or bridj.

献世佛 2024-12-13 01:52:37

回答我自己的问题:

http://java4cpp.kapott.org/

似乎不是一个活跃的项目。作者建议不要与 JDK 1.5 或更高版本一起使用。

它似乎有一个严重的问题:它将裸指针传递给它的包装器对象:

java::lang::Integer* i = new java::lang::Integer("10");

delete i; // don't forget to do this!

它还导致了一个更微妙的问题,为了表示赋值兼容性(例如,一个类作为它实现的接口的子类型),包装器必须继承来自彼此。

Answering my own question:

http://java4cpp.kapott.org/

Doesn't appear to be an active project. The author recommends it not be used with JDK 1.5 or later.

It appears to have a serious problem: it passes around naked pointers to its wrapper objects:

java::lang::Integer* i = new java::lang::Integer("10");

delete i; // don't forget to do this!

It also causes a more subtle problem that in order to represent assignment compatibility (e.g. a class as a subtype of the interface it implements) the wrappers have to inherit from each other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文