Attach to already running JVM(附加到已经运行的 JVM)
问题描述
有没有办法附加到已经运行的 JVM?
Is there a way to attach to an already running JVM?
例如,在 JNI 中,您可以使用 JNI_CreateJavaVM
创建 VM 并运行 jar 并检查其所有类..
For example, in JNI you can use JNI_CreateJavaVM
to create a VM and run a jar and inspect all its classes..
但是,如果 jar 已经在运行,我无法找到附加到其 JVM 并与其类进行通信或获取其 env
指针的方法..
However, if the jar is already running, I cannot find a way to attach to its JVM and communicate with its classes or get its env
pointer..
另一个问题是,如果 jar 加载了我的本机库 (.dll) 并且我想在 .dll 中创建一个 JVM,我不能......我也不能在没有 jar 调用我的函数的情况下附加 jar 的当前 JVM..
Another problem is that if the jar loads my native library (.dll) and I want to create a JVM inside the .dll, I cannot.. Nor can I attach the jar's current JVM either without the jar calling my function..
Java 端的示例:
class Foo
{
static {loadLibrary("Foo")}
}
在 C++ 方面:
void Foo()
{
//CreateJVM
//Attach to the current process..
//Call function from the jar that loaded me.
}
如果 jar 不先调用 Foo
,则无法做到这一点.
This cannot be done without the jar calling Foo
first.
有什么想法吗?有没有办法获取当前的 JVM 或附加到它或外部的 jvm 实例?
Any ideas? Is there no way to get the current JVM or to attach to it or an external jvm instance?
推荐答案
可以.
1) 在托管 JVM 的进程中注入 DLL(例如,java.exe
、javaw.exe
或 >iexplore.exe
).一种常见的注入技术是使用 SetWindowsHookEx
1) Inject a DLL in the process hosting the JVM (eg, java.exe
, or javaw.exe
, or iexplore.exe
). A common injection technique is to use SetWindowsHookEx
2)在DLL中,使用GetModuleHandle
3) 获取JNI_GetCreatedJavaVMs
函数的地址,使用GetProcAddress
3) Get the address of the JNI_GetCreatedJavaVMs
function, using GetProcAddress
4) 调用函数,如果成功,则使用 JavaVM
AttachCurrentThread 函数指针将线程附加到找到的第一个 JVM> 结构.
4) Call the function and, if successfull, attach your thread to the first JVM found, using the AttachCurrentThread
function pointer from the JavaVM
struture.
5) 完成.
有用的链接:调用 API
这篇关于附加到已经运行的 JVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:附加到已经运行的 JVM


基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 多个组件的复杂布局 2022-01-01