aspose file tools*
The moose likes Java in General and the fly likes bridge C++/Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "bridge C++/Java?" Watch "bridge C++/Java?" New topic
Author

bridge C++/Java?

manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308
has anybody experience with a library/product which enable communication between C++ and Java code (both directions). C++ must call Java code and pass callback (as object) so C++ program gets notified.
something like:
public void callJava(Object callbackPassedByCppCode);

google showed me different solutions. JNI made the best (first) impression on me. same opinion?

besides: CORBA is not an option to me.

or maybe for my C++ integration requirements i should use .Net?
(target api exists both as Java and .Net).

thanks


aldana software engineering blog & .more
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

It's called JNI, the Java Native Interface. Learn about it here.


[Jess in Action][AskingGoodQuestions]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
There are many ways that Java and C++ code can communiate with each other. However, JNI is different to all the others. With JNI, your Java and C++ code live in the same process and can call each other directly. With all other options, they live in different processes.

Therefore, you need to decide whether you want your C++ and Java in the same, or different process. If they must be in the same process, then JNI is the single available solution. If they can or must be in different processes, then there are lots of other solutions (CORBA, raw sockets, RPC, XML-RPC...), although actually most of them turn out to be using sockets underneath.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Alexandr Shvedov
Greenhorn

Joined: Jul 12, 2006
Posts: 23
Hello!
Actually, I have the same problem of connection two different processes, C++ and Java. I decided to use COM, but it doesn't look easy to me. Look atThis topic I was created recently.
Well, are there any real working techniques to organize data exchange beetween C++ and Java? (This is different processes, so JNI won't work).
manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308
thanks for your answers.

one thing is not clear to me:
[...] they live in different processes


of course i know what processes of OSs view are, but what do you mean exactly with it? my interpretation:
one process: starting Java implicitly loads C++ code. you don't need interprocesscommuniation, it just feels that your calling another api without for instance going down network stack.
multiple process: you have to execute both JVM and C++ binary (e.g. as COM) explicitly.

is that correct?
if so, i think i am going with JNI, because i am developing a Acrobat Plugin which needs to call Java (or .Net) on the same machine. and Java part does not act as a server, i.e. from application view it is directly connected to Acrobat.
Alexandr Shvedov
Greenhorn

Joined: Jul 12, 2006
Posts: 23
The fact is when we say about Java program it actually mean that there is C(C++) process (also known as JVM), which executes Java bytecode. When you want to use another C++ code you must compile it as DLL and then map it to JVM address space. In that case, c++ code will be executed in JVM process space, means that this two parts of code will live in one process = JVM. This process may be initiated only from Java side by calling native method of Java program. If you want to connect a different C(C++) program with Java then you'll have the same problem as me: this two parts of code is in different processes, so there is no way to connect them easy.
I have problem with it, because C++ program initiate data exchange, not java. Also C++ code is not packed as DLL, so it cannot be mapped to JVM process. I think the solution lays somewhere in using COM of CORBA technologies, but I am not very good with them yet. So, if anybody have any ideas or sample code it would be great if you can help us.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Dmitry Popov:
This process may be initiated only from Java side by calling native method of Java program.


Not true. Your C++ program can fire up a JVM then ask it to run some Java. This JVM will be part of the same process as the C++ program.

Search for Java Invocation Interface.
[ July 14, 2006: Message edited by: Peter Chase ]
Alexandr Shvedov
Greenhorn

Joined: Jul 12, 2006
Posts: 23
Your C++ program can fire up a JVM then ask it to run some Java

You're right. You can embend JVM in your application and then start it, get the pointer to it and then work with it. But as far as I understand this won't help you if you have already lanched a copy of JVM, and start java program. Then you cannot get reference to it from C++ program. Am I right or not?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
Well, are there any real working techniques to organize data exchange beetween C++ and Java?

Short answer: Yes, loads of them! See Peter Chase's list. At the top end as far as generality and complexity we have SOAP - at the bottom end we have UDP. In order to zero in on your solution, you need to refine the statement of the problem.
Bill
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Dmitry Popov:

You're right. You can embend JVM in your application and then start it, get the pointer to it and then work with it. But as far as I understand this won't help you if you have already lanched a copy of JVM, and start java program. Then you cannot get reference to it from C++ program. Am I right or not?


Well, of course you are. If you start two separate processes, then you can't call directly between them. That's what processes are for: to have isolated, protected execution. Imagine the security nightmare if any process could just call some other unrelated process. Early computers allowed it, and maybe some specialised current platforms still do, but not current general-purpose computers (anything running Windows, Unix, MacOSX etc.).

As I and others have mentioned, there are lots of ways of making calls between processes, but none of them involve directly calling the code in the other process, because that's impossible. That said, once you have the infrastructure in place, many of the methods make it look like you're directly calling methods on a remote object. You aren't, though, there are proxies, sockets, stubs and whatever in between to achieve the illusion.
Alexandr Shvedov
Greenhorn

Joined: Jul 12, 2006
Posts: 23
Short answer: Yes, loads of them! See Peter Chase's list.

Well, I am the begginer here, what is the "Peter Chase's list" and where can I find it?
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Originally posted by Dmitry Popov:

Well, I am the begginer here, what is the "Peter Chase's list" and where can I find it?


Scroll up in this thread.
[ July 14, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Alexandr Shvedov
Greenhorn

Joined: Jul 12, 2006
Posts: 23
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bridge C++/Java?