使用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
有人知道这样做的正确方法是什么?
答案 0 :(得分:2)
请参阅
中的“// 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");
}