gpt4 book ai didi

java - 使用 Attach API 连接到 J9VM

转载 作者:行者123 更新时间:2023-11-30 04:54:13 25 4
gpt4 key购买 nike

使用 Java Attach API 连接到 IBM J9VM 的正确方法是什么?

我正在尝试以下操作(在我的类路径中包含 JDK 的tools.jar):

private static final J9AttachProvider ATTACH_PROVIDER = new J9AttachProvider();


...snip...
String pid = getPIDofRunningVM();
Constructor<J9VirtualMachine> constructor = J9VirtualMachine.class.getConstructor(new Class[]{AttachProvider.class, String.class
});
constructor.setAccessible(true);
J9VirtualMachine virtualMachine = constructor.newInstance(new Object[]{ATTACH_PROVIDER, pid});

我也尝试过不使用反射来做到这一点,但我不断得到

Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:44)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:516)
at de.test.Test.gogo(Test.java:34)
at de.teset.Test.main(Test.java:26)
Caused by: java.lang.IllegalAccessError
at ibm.tools.attach.J9VirtualMachine.<init>(J9VirtualMachine.java:24)
... 6 more

有人知道执行此操作的正确方法是什么吗?

最佳答案

查看里面的“//IBM”部分

     private static final String CONNECTOR_ADDRESS =
"com.sun.management.jmxremote.localConnectorAddress";

void retrieveSomeMXBean (String pid) throws Exception {
com.sun.tools.attach.VirtualMachine vm =
com.sun.tools.attach.VirtualMachine
.attach(pid);
String connectorAddress = vm.getAgentProperties().getProperty(
CONNECTOR_ADDRESS);


if (connectorAddress == null) {
String agent = vm.getSystemProperties().getProperty("java.home")
+ File.separator + "lib" + File.separator
+ "management-agent.jar";
vm.loadAgent(agent);

connectorAddress = vm.getAgentProperties().getProperty(
CONNECTOR_ADDRESS);
}

// IBM
if (connectorAddress == null) {

String agent = "instrument,"
+ vm.getSystemProperties().getProperty("java.home")
+ File.separator + "lib" + File.separator
+ "management-agent.jar=";
vm.loadAgentLibrary(agent);

connectorAddress = vm.getSystemProperties().getProperty(
CONNECTOR_ADDRESS);
}

JMXServiceURL url = new JMXServiceURL(connectorAddress);
JMXConnector connector = JMXConnectorFactory.connect(url);
mbeanConn = connector.getMBeanServerConnection();

memoryMXBean = new ObjectName("java.lang:type=Memory");

}

关于java - 使用 Attach API 连接到 J9VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9081752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com