Reputation: 347
What is the correct way to attach to IBM's J9VM using the Java Attach API?
I am trying the following (with having the JDK's tools.jar in my classpath):
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});
I also tried doing it without the use of reflection but i keep getting
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
Does anybody know what the proper way to do this is?
Upvotes: 1
Views: 845
Reputation: 21
see the "//IBM" part inside
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");
}
Upvotes: 2