JNI question

R

Rajeev Madan

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.

Thanks
Rajeev
 
J

Jim Sculley

Rajeev said:
I have a JNI question. Is is possible for a native code to attach to a
running JVM and invoke some methods on it.

You don't invoke methods on a JVM.

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.

I don't think you can do what you want to do. A running JVM consists of
several threads, doing any number of things at a given moment.
Arbitrarily hijacking one of these threads to handle some call from
native code would be impossible.

What you would need to do is set up a client/server architecture where
the Java code has a thread listening on a socket and then the native
code can use the socket as an entry point into the running Java code.

Jim S.
 
R

Roedy Green

Is is possible for a native code to attach to a
running JVM and invoke some methods on it.

Native code can call Java methods and vice versa. Native code can
exec a new JVM. You will need some JNI glue in a DLL that Java loads
to get the interchange started.
 
J

Joseph Millar

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
 
R

Rajeev Madan

Let me explain my problem more clearly. We have a Java application (on Linux
platform) that we want to test. For testing, we have some C code. So, from
the C code, we want to execute Java methods. Our assumption was that if Java
application was running, we could launch the testing application which can
then attach to the JVM running the Java applications and then can execute
these Java methods.

From the messages, it seems likely that this is not possible. We will have
to have the test application launch the Java application and then it can
invoke the necessary Java methods.

Rajeev
 
J

Jon A. Cruz

Rajeev said:
From the messages, it seems likely that this is not possible. We will have
to have the test application launch the Java application and then it can
invoke the necessary Java methods.

No. JNI by itself can't do that.


However... you might be able to get what you want from a combination of
things.

Take a look at JVMPI and JPDA under "Tool Support" in the JDK docs.

Things under JPDA cover attaching to a running Java VM. At least some
people out there have used it to create new objects and invoke methods.



So it seems as if people were sticking to your question precisely as it
was phrased, instead of noticing that you might achieve your desired
ends through other standard Java means.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top