tags:

views:

221

answers:

3

Hi, I'm making a lib so that cpp apps can communicate with the JVM. Suppose that the JVM has already started how can I make a cpp binary communicate with the JVM? I think the best solution is to store the JNI env variable in the shared object (so) so I can include it on the cpp and use later.

Is that possible?

EDIT ----

I want to get the JavaVM Interface outside of the JVM, something like this method:

  • GetJavaVM returns the JavaVM interface pointer for the current virtual machine instance.
A: 

No, I don't think so. The JVM gets the environment at the time of its starting, and it can't be changed from an outside process during the program's runtime.

Standard procedure these days is for programs to communicate using TCP/IP and sockets. For a simple and cheap solution, you could consider using files in a directory (though there are performance and concurrency problems with this, of course).


I just noticed the "I'm making a lib" statement. If your lib is for "public" consumption, it should be cross-platform, and then I think socket I/O would be the thing to do.

You could possibly hijack JMX to do what you're asking for, but I'm not very knowledgeable on that.

Carl Smotricz
+1  A: 

Your question is unclear: it sounds like you want a C++ application to communicate with a JVM running as a separate process. In which case you need to use some form of inter-process communication such as pipes, sockets, CORBA, whatever. The JNIEnv pointer, like all pointers, is only valid within the process in which it's used.

The only case that I think fits your question is if you start a Java program, call into a native method, then that native method starts up separate threads. In which case, no, you can't share the JNIEnv pointer, because it's tied to a thread. However, you can use the JNI invocation API to access the Java VM from your C++ thread.

kdgregory
That's what I want, I have 2 processe one with the JVM with a native lib that will store the pointer and another one with only c++ code that will use this lib. I want the other process (C++ only) to use the lib and call the jvm (other process).
Marcos Roriz
In case it wasn't clear from my post, you can't do that.
kdgregory
I think maybe is possible to make a method 'connect' in the native lib to do the following:* Return the JavaVM pointer with GetJavaVM()Now call the AttachCurrentThread() with that JavaVM pointer.My question is if that possible since they aren't separated by threads but by process :(
Marcos Roriz
Please reread my answer, and the answers of everyone else who responded. And if you still believe it is possible, then please read a book on operating system implementation.
kdgregory
And if in the process you come across a reference to "shared memory" and think that it will solve your problems: no, it won't.
kdgregory
So best approach is do with inter process communication, ? :(
Marcos Roriz
A: 

How many processes are you in here? If this is all inside one process, then the answer is 'yes'. C++ can call the GetEnv API from jni.h.

bmargulies
It's 2 process :(
Marcos Roriz
Time to create a web service in Java that can be spoken to from C++?
bmargulies