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


- 如何使用WebFilter实现授权头检查 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01