附加到已经运行的 JVM

2023-08-23Java开发问题
1

本文介绍了附加到已经运行的 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.exejavaw.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) 调用函数,如果成功,则使用 JavaVMAttachCurrentThread 函数指针将线程附加到找到的第一个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13