tags:

views:

371

answers:

3

Hi,

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

+3  A: 

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.

Tom Hawtin - tackline
Tom, can you include a link to Joel's "rant on simplicity?" I'm curious as to what he said.
rtperson
I couldn't find any official representation but look here and you'll find a lot of discussion on the topic. http://tr.im/HCoB
Peter Lindqvist
OP should look at REST first, maybe the naivety of REST isn't a problem for OP's situation. RMI is basically connection-less, and REST should be capable if OP thought RMI can do the job.
irreputable
+1  A: 

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.

Jherico
A: 

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)

Cheeso
My only disagreement would be around long term maintenance of the code. I've seen a few programs that do this and they tend to forget to expose command line arguments and other JVM features that might need to be set in different environments. While it may be an extra process, a standalone JVM is usually easier to manage both by the development team and the administrative team.
Jim Rush
my impression was that JNI is for java-to-C++ calls. Does it work the other way round, i.e. C++-to-Java? This should effectively mean that it runs the JVM underneath.
davka
Yes, it works the other way round, too. I added the link to the relevant JDK doc on the topic, above.
Cheeso