开发者

execute new java code in existing jvm process

开发者 https://www.devze.com 2022-12-17 20:08 出处:网络
I have a java process currently running under a windows shell. One of the threads res开发者_高级运维ponsible for serialisation is blocked indefinitely and as a result important information which is s

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

One of the threads res开发者_高级运维ponsible 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.

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


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

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.

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

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.

Good luck!!


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

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消