Any ideas or suggections are welcome.
Thanks
Rajeev
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.
--
Remove my extraneous mandibular appendages to reply via e-mail.
Maybe, depending on what exactly you want to do and what the environment
is. There is a call, JNI_GetCreatedJavaVMs, which returns pointers to all
the virtual machine instances. But I don't know the scope... perhaps this
is only within a single process. Are you trying to connect externally?
One issue is that somehow the native code has to get executed. If the
native code didn't create the JVM in the first place, then the JVM executes
first and invokes the native code only when there's a native method.
--Marc
>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.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
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
"Roedy Green" <ro...@mindprod.com> wrote in message
news:dm4bgv4t9j8jspvjv...@4ax.com...
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.
> 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.
Correct, JNI will not help you directly there as you would like. How
about something like this? A new Java application creates two threads,
one of which is is used to call and give control to the C code using
JNI, the other which runs the old application's main() method. In the
C code, you can now use JNI to call methods of the original application.
This all depends on exactly what context these executed Java methods
are run in as you have them running in a diffent thread, but inside the
same JVM, so their static date is shared and have access to same object
references as the application, but you have cuncurrency issues now.
I'm still pretty fuzzy on exactly what you are trying do. The above is
going to depend highly on how the C code is structured and exactly what
that code is trying to do. Along with exactly how the Java methods
expect the environment to be.
Hope this helps.
--Joe
> comp.lang.java.programmer,comp.lang.java.help
This seems to be a good candidate for python..