I have a JNI question. Is is possible for a native code to attach to a
running JVM and invoke some methods on it. I have searched for this but
haven't come across any answers. Most of the articles talk about invoking a
JVM within the native code and then invoking methods on it. I just want to
do it a different way.
Any ideas or suggections are welcome.
If I understand you correctly, then no. The JNI api's provide no
built in method for someone outside the process to control the JVM
to my knowledge. Now, if the app encompassing the JVM or the code
being run by the JVM were coded to accept outside requests, then
that would work using such facilites as sockets, RMI, shared memory,
CORBA, etc. In other words you would have to add your own piece
of code to the process running the JVM and it would have to know
how to respond to outside requests of some kind.
There are techniques on Win32 for inserting and running a DLL into
another already running process, but they are by no means simple and
easy to do. They are typically used to do some sort of inspection
or instrumentation of external code. This might be very interesting
to try from a research standpoint. I do not know if similar methods
exist on other platforms.
It would seem much easier to provide your own wrapper class for this
other application and have it listen in a new thread for incoming
requests. So when the Java application is run, your class runs,
forks the thread, then runs the original app. Another approach is
to roll your own java launcher that provides the interface you
what before the actual start of the JVM via the invocation API).
If you gave us an idea of what type of things you're trying to do,
we might have other ideas, but if it involves JNI, they tend to be
very platform specific.
Good luck.
--Joe