java - 使用 Attach API 连接到 J9VM

标签 java j9

使用 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/

相关文章:

java - @RequestBody在Spring中始终为空

java - Lob 使用 Play Framework 和 Ebean 和 H2 返回 null

java - 使用 hprof 分析 WebSphere 的 CPU SAMPLES 输出

java - 在哪里可以找到特定版本的 IBM JDK/JRE for Windows?

java - JProgressBar 不会更新

Java:java.util.NoSuchElementException:找不到行

java - 如何获取在 J9 VM (Linux) 上打印的调试信息?

windows-mobile - 为 Windows Mobile 创建一个安装程序

java - Guice 多注解