开发者

C++ client for Java RMI? Or any other way to use Java from C++?

开发者 https://www.devze.com 2022-12-13 20:36 出处:网络
We need to use a Java library from C++ code. An idea that I had is that if we could build a C++ client for Java RMI (ideally using some fr开发者_C百科amework or wizard), than we could run the Java lib

We need to use a Java library from C++ code. An idea that I had is that if we could build a C++ client for Java RMI (ideally using some fr开发者_C百科amework or wizard), than we could run the Java lib as a separate server. This seem cleaner than trying to run Java VM within a C++ application.

Alternatively, if you have any other idea on how to use Java from C++, I'd be glad to hear. We work on Linux.

thanks a lot,

David


RMI is intimately linked with the JVM (and Java serialisation), so that isn't reasonable. Not unless the C++ client includes a JVM.

CORBA is the obvious platform-independent equivalent. It is a bit design-by-committee and is now considered very untrendy. WS-/JAX-WS is kind of the modern equivalent with lots of XML, but may be considered a bad attempt at CORBA. REST is an attempt at a lightweight WS-, but see Joel's Stackoverflow DevDay rant on claimed "simplicity".

You could go old school and just shove byte over TCP/IP (or pipes). Or if local, just exec the C++ program. Or use a native interface: JNI built into the JRE, or JNA a nicer layer over the top.


Don't bother with RMI. If you're willing to take the step of making the Java application a separate server, have your C++ client communicate via JMS (Java Messaging Service). ActiveMQ is a free JMS message broker implementation that provides embedded services as well as C++ client libraries. The JMS protocol is dead simple to use (at least from Java). Its probably not as flexible as doing REST, but it would likely be an easier implementation.


JNI was the intended solution to the problem of C/C++ to Java integration. It's not difficult.

Message Queues are better for larger grained interactions, or remote interactions where the message queue is accessible over the network.

CORBA and RMI were also intended to be network access mechanisms.

From your description you don't want that. You want to consume a Java library in C++, and to do that, you use JNI.

How to start the JVM and invoke a Java method, from C++ (JDK doc)

0

精彩评论

暂无评论...
验证码 换一张
取 消