问题描述
我有一个必须与jdk 1.5一起运行的Java应用程序。我需要一种使用其PID附加到此应用程序JVM的方法。 我尝试了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)
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());
}
}
有人可以帮助我解决这个问题吗?
解决方法
Attach API在JDK 6中引入。您可以在软件包 def store(self,item):
"""Stores an item in the hashtable."""
index_initial = self._hash(item)
index_current = self._hash(item)
i_counter = 0
if not self.__contains__(item):
slot_found = False
while slot_found == False:
if self._data[index_current] == None:
self._data[index_current] = item
slot_found = True
else:
index_current = index_initial + i_counter
i_counter += 1
if index_current == self.n_slots:
index_current = 1
# Keep track of number of items in hash table
self.n_items += 1
中找到它,该软件包在JDK 5中不可用。如果您的JDK文件夹中没有相应的库com.sun.tools.attach
(类似于UNIX的操作系统)或jre/lib/.../libattach.so
(Windows),您也可以在其中找到它。您想将JDK 5与6+进行比较。因此,您无法使用此方法将代理热附加到Java 5 VM。也许您可以在Java 6+ VM上运行Java 5应用程序,然后将其附加到该VM。
PS:您不需要ByteBuddy即可热附加代理,请参见this tutorial:
jre/bin/attach.dll
针对在Java 5 VM中运行的程序运行此命令时,您还将看到更具体的错误消息:
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();
}
}
,
从堆栈跟踪中,我可以看到您正在使用VirtualMachine$ForHotSpot$Connection$ForJnaWindowsNamedPipe
。这意味着Byte Buddy正在使用JNA为您模拟附件。虽然在不提供附件提供程序的Java 5中是可行的,但目标VM必须至少是版本6,在该版本中引入了动态附件以调度要附加的命令。
如果可以使用JNA,则无法将其附加到版本5 VM。