在现有的jvm进程中执行新的java代码

时间:2022-12-24 17:19:01

I have a java process currently running under a windows shell.

我有一个java进程正在windows shell下运行。

One of the threads responsible for serialisation is blocked indefinitely and as a result important information which is stored in memory is no longer being written to disk.

负责序列化的一个线程被无限期地阻塞,因此存储在内存中的重要信息不再被写入磁盘。

If I shutdown the process, the information will be lost.

如果我关闭进程,信息将丢失。

It would be convenient if I could write and compile some new code and have it execute in the same memory space so that the said information could be serialised once more before I shutdown the process.

如果我可以编写和编译一些新代码,并让它在相同的内存空间中执行,以便在我关闭进程之前,可以再次对上述信息进行序列化,这将非常方便。

The process was started using a java -jar command.

使用java -jar命令启动进程。

With the hotspot VM features, is there any way to achieve this?

有了hotspot VM的特性,有什么办法实现这一点吗?

2 个解决方案

#1


12  

You can use the Attach API to attach to a virtual machine. Here's an article that explains how to use it

可以使用Attach API将其附加到虚拟机。这是一篇解释如何使用它的文章

Here's a code example:

这里有一个代码示例:

String agentJAR = "myAgent.jar";
VirtualMachine vm = VirtualMachine.attach (processid);
vm.loadAgent(agentJAR);

Where the agent is the name of your jar.

代理是jar的名称。

The agent jar contains an Agent, which can interface with the JVM using the Instrumentation API.

代理jar包含一个代理,它可以使用插装API与JVM进行接口。

To create an agent that gets loaded at runtime, you implement an agentmain function like this:

要创建在运行时加载的代理,需要实现如下的代理主函数:

public static void agentmain(String agentArgs, Instrumentation inst); 

or

public static void agentmain(String agentArgs); 

The Instrumentation object is used to modify classes at runtime, which you probably don't need. But hopefully you can just put whatever code you need to run in agentmain and then use the attach API to run it in the target JVM.

Instrumentation对象用于在运行时修改类,这可能不需要。但是希望您可以将需要运行的代码放在agentmain中,然后使用attach API在目标JVM中运行它。

Good luck!!

祝你好运! !

#2


0  

You might try registering a signal handler, this is more limited on Windows than on other platforms.

您可能尝试注册一个信号处理程序,这在Windows上比在其他平台上更加有限。

Examples and description http://www.ibm.com/developerworks/java/library/i-signalhandling/

例子和描述http://www.ibm.com/developerworks/java/library/i-signalhandling/

But the question to ask is why is the thread blocked?

但要问的问题是,为什么线程被阻塞?

#1


12  

You can use the Attach API to attach to a virtual machine. Here's an article that explains how to use it

可以使用Attach API将其附加到虚拟机。这是一篇解释如何使用它的文章

Here's a code example:

这里有一个代码示例:

String agentJAR = "myAgent.jar";
VirtualMachine vm = VirtualMachine.attach (processid);
vm.loadAgent(agentJAR);

Where the agent is the name of your jar.

代理是jar的名称。

The agent jar contains an Agent, which can interface with the JVM using the Instrumentation API.

代理jar包含一个代理,它可以使用插装API与JVM进行接口。

To create an agent that gets loaded at runtime, you implement an agentmain function like this:

要创建在运行时加载的代理,需要实现如下的代理主函数:

public static void agentmain(String agentArgs, Instrumentation inst); 

or

public static void agentmain(String agentArgs); 

The Instrumentation object is used to modify classes at runtime, which you probably don't need. But hopefully you can just put whatever code you need to run in agentmain and then use the attach API to run it in the target JVM.

Instrumentation对象用于在运行时修改类,这可能不需要。但是希望您可以将需要运行的代码放在agentmain中,然后使用attach API在目标JVM中运行它。

Good luck!!

祝你好运! !

#2


0  

You might try registering a signal handler, this is more limited on Windows than on other platforms.

您可能尝试注册一个信号处理程序,这在Windows上比在其他平台上更加有限。

Examples and description http://www.ibm.com/developerworks/java/library/i-signalhandling/

例子和描述http://www.ibm.com/developerworks/java/library/i-signalhandling/

But the question to ask is why is the thread blocked?

但要问的问题是,为什么线程被阻塞?