通过JNI将本机应用程序附加到Linux上已在运行的JVM [英] Attach native application via JNI to already running JVM on Linux

查看:121
本文介绍了通过JNI将本机应用程序附加到Linux上已在运行的JVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题已在Windows中得到解答,但我找不到了解如何在Linux上实现相同的目标.

A similar question has already been answered for Windows, but I could not find out how to achieve the same on Linux.

我想加深我对JNI的了解.我已经使用 JNI_CreateJavaVM ,从本机应用程序中创建一个新的JVM.

I want to deepen my knowledge in JNI. I already got JNI projects working with JNI_CreateJavaVM, creating a new JVM from within the native application.

但是这次,我想在本机应用程序中创建JVM(即, not 使用 AttachCurrentThread 通过某个java myApplication调用在本机应用程序之前启动的VM).

But this time I would like to not create the JVM within the native application (i.e., not using JNI_CreateJavaVM), but to attach to an already running one (i.e., using AttachCurrentThread on a VM that has been started before the native application by some java myApplication call).

Linux上是否有一种方法可以实现此目的?我需要获取正在运行的JVM的JavaVM对象.我尝试使用 JNI_GetCreatedJavaVMs ,但这不会返回任何JVM(我认为此方法仅返回当前进程创建的VM,例如,通过使用

Is there a way on Linux how to achieve this? I need to get a JavaVM object of the running JVM. I tried to use JNI_GetCreatedJavaVMs, but this does not return any JVMs (I think this method only returns VMs created by the current process, e.g., by using JNI_CreateJavaVM, and not all VMs that are running on the system)

推荐答案

JNI函数只能在启动JVM的进程中使用. JNI不允许您控制其他进程.

JNI functions can be used only within the process that started JVM. JNI does not allow you to control other processes.

但是,有一种方法可以使用HotSpot动态附加API在不同的JVM进程的上下文中加载代码.

However, there is a way to load your code in the context of different JVM process using HotSpot Dynamic Attach API.

  • Compile your code into an agent library (.so);
  • Create Agent_OnAttach function that will be an entry point for your code;
  • Load the agent library using Dynamic Attach.

jattach 项目中.

There is Java API to attach to remote JVM and to load agent library in its context. But you can also do it from native code like in my jattach project.

这篇关于通过JNI将本机应用程序附加到Linux上已在运行的JVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆