无法使用jdk 1.5附加到JVM

huangapple go评论63阅读模式
英文:

Cannot Attach to JVM using jdk 1.5

问题

我有一个必须在jdk 1.5下运行的Java应用程序,我需要一种方法来附加到该应用程序的JVM,使用它的PID。
我尝试了ByteBuddy库,但在尝试加载代理时出现以下错误。

Exception in thread "main" java.lang.IllegalStateException: Target could not dispatch command successfully
    at net.bytebuddy.agent.VirtualMachine$ForHotSpot$Connection$ForJnaWindowsNamedPipe.execute(VirtualMachine.java:1043)
    at net.bytebuddy.agent.VirtualMachine$ForHotSpot.load(VirtualMachine.java:361)
    at net.bytebuddy.agent.VirtualMachine$ForHotSpot.loadAgent(VirtualMachine.java:335)
    at main.Agent.main(Agent.java:28)

以下是main方法中的代码:

public static void main(String[] args) {
    try {
        VirtualMachine vm = VirtualMachine.ForHotSpot.attach("19708");
        vm.loadAgent("Agent.jar");
        vm.detach();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

有人能帮我解决这个问题吗?

英文:

I have a java application that must run with jdk 1.5. I need a way to attach to this application JVM using its PID.
I tried ByteBuddy library but it is giving me the following error when trying to load the agent.

Exception in thread "main" java.lang.IllegalStateException: Target could not dispatch command successfully
at net.bytebuddy.agent.VirtualMachine$ForHotSpot$Connection$ForJnaWindowsNamedPipe.execute(VirtualMachine.java:1043)
at net.bytebuddy.agent.VirtualMachine$ForHotSpot.load(VirtualMachine.java:361)
at net.bytebuddy.agent.VirtualMachine$ForHotSpot.loadAgent(VirtualMachine.java:335)
at main.Agent.main(Agent.java:28)

Here's the code in the main method:

public static void main(String[] args) {
	try {
		VirtualMachine vm = VirtualMachine.ForHotSpot.attach("19708");
		vm.loadAgent("Agent.jar");
		vm.detach();
	} catch (IOException e) {
		System.out.println(e.getMessage());
	}
}

Can anyone help me with this issue?

答案1

得分: 1

Attach API是在JDK 6中引入的。您可以在com.sun.tools.attach包中找到它,在JDK 5中不可用。此外,相应的库jre/lib/.../libattach.so(类UNIX操作系统)或jre/bin/attach.dll(Windows)在您的JDK文件夹中也不可用,如果您想将JDK 5与6+进行比较。因此,您无法使用此方法将代理程序热附加到Java 5 VM。也许您可以在Java 6+ VM上运行您的Java 5应用,然后附加到该VM。

P.S.:为了进行热附加代理程序,您不需要使用ByteBuddy,请参阅这个教程

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

import java.io.File;
import java.io.IOException;

class Scratch {
  public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
    VirtualMachine jvm = VirtualMachine.attach("22384");
    jvm.loadAgent(new File("foo.jar").getAbsolutePath());
    jvm.detach();
  }
}

当针对在Java 5 VM中运行的程序运行此代码时,您还将看到一个更具体的错误消息:

Exception in thread "main" com.sun.tools.attach.AttachNotSupportedException: The VM does not support the attach mechanism
	at sun.tools.attach.HotSpotAttachProvider.testAttachable(HotSpotAttachProvider.java:162)
	at sun.tools.attach.WindowsAttachProvider.attachVirtualMachine(WindowsAttachProvider.java:67)
	at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208)
	at Scratch.main(scratch.java:11)
英文:

The Attach API was introduced in JDK 6. You find it in package com.sun.tools.attach, which is unavailable in JDK 5. Also the corresponding library jre/lib/.../libattach.so (UNIX-like OS) or jre/bin/attach.dll (Windows) is unavailable in your JDK folder, if you want to compare JDK 5 against 6+. Hence, you cannot hot-attach an agent with this method to a Java 5 VM. Maybe you could run your Java 5 application on a Java 6+ VM and then attach to that one.

P.S.: You don't need ByteBuddy in order to hot-attach an agent, see this tutorial:

import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;

import java.io.File;
import java.io.IOException;

class Scratch {
  public static void main(String[] args) throws IOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException {
    VirtualMachine jvm = VirtualMachine.attach("22384");
    jvm.loadAgent(new File("foo.jar").getAbsolutePath());
    jvm.detach();
  }
}

When running this against a program running in a Java 5 VM, you will also see a more specific error message:

Exception in thread "main" com.sun.tools.attach.AttachNotSupportedException: The VM does not support the attach mechanism
	at sun.tools.attach.HotSpotAttachProvider.testAttachable(HotSpotAttachProvider.java:162)
	at sun.tools.attach.WindowsAttachProvider.attachVirtualMachine(WindowsAttachProvider.java:67)
	at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:208)
	at Scratch.main(scratch.java:11)

答案2

得分: 0

从堆栈跟踪中可以看出,您正在使用 VirtualMachine$ForHotSpot$Connection$ForJnaWindowsNamedPipe。这意味着 Byte Buddy 正在使用 JNA 为您模拟附加操作。虽然在 Java 5 中这是可能的,因为它不提供附加程序提供者,但目标虚拟机必须至少是版本 6,因为版本 6 引入了动态附加以将命令分派到附加操作。

不可能附加到版本 5 的虚拟机,只能从具有可用 JNA 的虚拟机进行附加操作。

英文:

From the stack trace, I can see that you are using VirtualMachine$ForHotSpot$Connection$ForJnaWindowsNamedPipe. This means Byte Buddy is emulating the attachment for you using JNA. While this is possible in Java 5 which does not offer an attachment provider, the target VM must be of at least version 6 where dynamic attachment was introduced to dispatch the command to attach.

It is not possible to attach to a version 5 VM, only from one if JNA is available.

huangapple
  • 本文由 发表于 2020年8月24日 23:20:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63563922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定