Attach to an already running JVM

Is there a way to connect to an already running JVM?

For example, in the JNI, you can use JNI_CreateJavaVM to create a virtual machine and run the bank and check all her classes.

However, if the jar is already running, I can not find a way to connect to its JVM and communicate with its classes or get it index env .

Another problem is that if the jar loads my home library (DLL), and I want to create a JVM inside the .dll, I can not .. I also can not connect the current JVM jar without a jar, calling my function ..

Example from Java:

 class Foo { static {loadLibrary("Foo")} } } class Foo { static {loadLibrary("Foo")} } 

on the side of C ++:

 void Foo() { //CreateJVM //Attach to the current process.. //Call function from the jar that loaded me. } 

It can not be done without calling the flag Foo .

Any ideas? There is a way to get the current JVM or attach a copy of or an external jvm?

+6
source share
2 answers

Yes, you can.

1) Introduce a DLL JVM placement process (e.g., java.exe or javaw.exe or iexplore.exe ). A common method is to use injection SetWindowsHookEx

2) . The DLL module handle to get jvm.dll using GetModuleHandle

3) Get the address of the function JNI_GetCreatedJavaVMs , using GetProcAddress

4) Call the function and, if successful, connect your stream to the first found the JVM, using a function pointer AttachCurrentThread of the line JavaVM .

5) Done.

Useful link: the API-interface Invocation

+14
source

No, you can’t. JNI allows exactly two models:

  • Your program is different from Java, create an JVM.
  • The Java program is a proprietary method.

If you need to communicate in other cases, you will need to use some other mechanism. Web services - this is a simple approach.

+1
source

All Articles